Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用UIButton作为切换按钮?_Ios_Iphone_Uibutton_Rubymotion_Rmq - Fatal编程技术网

Ios 如何使用UIButton作为切换按钮?

Ios 如何使用UIButton作为切换按钮?,ios,iphone,uibutton,rubymotion,rmq,Ios,Iphone,Uibutton,Rubymotion,Rmq,我正在尝试为表中的每个单元格创建一个切换按钮。按下时,它将更改图像,再次按下时,它将再次更改图像-切换 在ui按钮类中,我看不到已选择状态 我正在寻找一种方法来创建一个带有UIButton的切换按钮,这样我就可以在每次单击时更改状态 这就是我现在在rubymotion中使用rmq @fav_button.on(:touch) do |sender| puts "pressed fav button for id: " + data[:id] + " and name: " + data[:n

我正在尝试为表中的每个单元格创建一个切换按钮。按下时,它将更改图像,再次按下时,它将再次更改图像-切换

ui按钮
类中,我看不到
已选择
状态

我正在寻找一种方法来创建一个带有UIButton的切换按钮,这样我就可以在每次单击时更改状态

这就是我现在在
rubymotion
中使用
rmq

@fav_button.on(:touch) do |sender|
  puts "pressed fav button for id: " + data[:id] + " and name: " + data[:name]
  #how do I change the state here?
end

您可以轻松创建切换按钮,只需为各自的状态设置相应的图像,然后,您可以使用
selected
属性在这些图像之间切换

我制作了一个纯objective-c代码来展示如何做到这一点,但您也可以在故事板中设置图像,请查看:

// First, set the images for normal state and selected state
[button setImage:normalImage forState:UIControlStateNormal];
[button setImage:selectedImage forState:UIControlStateSelected];



我希望这能对您有所帮助。

因为您的问题确实提到了rmq,下面是一种rmq方法:

viewDidLoad
中:

@hello_world_label = rmq.append(UILabel, :hello_world).get
@button            = rmq.append(UIButton, :toggleable_button)

@button.on(:touch) do |sender|
  sender.selected = !sender.selected?
end
请注意,切换是通过查询按钮的实际状态来实现的。如果需要记住这一点以便以后使用,可能需要将其保存在实例变量中

在样式表中:

def toggleable_button(st)
  st.frame = {t: 200, w: 100, h: 24}
  st.image_normal = image.resource('toggle_me')
  st.image_selected = image.resource('toggled')
end
注意所选图像的使用。嗯,这在rmq中不存在,但是您可以很容易地实现。如果这是一个rmq项目,您将拥有一个stylers/目录。在这里,您应该看到ui_button_styler.rb。以下是使突出显示的州成为一等公民的代码:

module RubyMotionQuery
  module Stylers
    class UIButtonStyler < UIControlStyler 

      def image_selected=(value)
        @view.setImage(value, forState:UIControlStateSelected)
      end
      def image_selected
        @view.imageForState UIControlStateSelected
      end

    end
  end
end
模块RubyMotionQuery
模块样式器
类UIButtonStyler

如您所见,您的控制器代码保持干净,按钮的初始设置转移到样式表中,并且您已整齐地扩展rmq以了解所选状态。

切换按钮图像的最简单逻辑。:)


你可以用一种非常简单的方法来做。首先,在
viewDidLoad
中为按钮设置标记。让我们假设
self.toggleButton.tag=111
。现在,在按钮操作功能中,您可以切换按钮,如下所示:

- (IBAction)toggling:(id)sender{
    if(self.toggleButton.tag == 111){
        //this is normal state
        [self.toggleButton setTitle:@"Less..." forState:UIControlStateNormal];
        self.toggleButton.tag = 222;
    }else{
        //selected state
        [self.toggleButton setTitle:@"More..." forState:UIControlStateNormal];
        self.toggleButton.tag = 111;
    }
}
您可以像这样更改按钮的图像
[self.toggleButton setImage://some 图像状态:uicontrol状态正常] Swift 4.0解决方案(使用故事板)

首先,确保属性检查器中的
ui按钮
类型设置为
Custom

下面的屏幕截图演示了对故事板中的
toggleButton
的引用,以及
事件内部的润色,即:用户点击按钮,按钮将触发下面的
didPressButton


嗨,谢谢你的回答。当您执行
button.selected=!已选择
其中是
!所选的
来自?如果我做了
button.selected=!button.selected
然后我得到一个错误很好的观察结果,我忘了为
selected
属性设置按钮范围。我还更改了第一个参数的名称,以避免与button实例冲突。现在这一点更有意义了。谢谢仍然需要对样品进行小修。您将按钮命名为“aButton”-而不是“button”。您可以修改以使示例可用吗?您还可以在属性检查器中设置selectedImage和normalImage。为此,您只需将“状态配置”从“默认”切换到“选定”,然后设置所需的图像资产。这样,您只需要按DIDPRESS按钮,而不需要viewDidLoad中的其他代码。
module RubyMotionQuery
  module Stylers
    class UIButtonStyler < UIControlStyler 

      def image_selected=(value)
        @view.setImage(value, forState:UIControlStateSelected)
      end
      def image_selected
        @view.imageForState UIControlStateSelected
      end

    end
  end
end
(IBAction)toggleButton:(id)sender {
       if (self.toggleButton.selected==YES) {
           [self.toggleButton setSelected:NO];
        }else{
       [self.toggleButton setSelected:YES];
       [self.toggleButton setImage:[UIImage imageNamed:@"favStar.png"]    forState:UIControlStateSelected];
       }
- (IBAction)toggling:(id)sender{
    if(self.toggleButton.tag == 111){
        //this is normal state
        [self.toggleButton setTitle:@"Less..." forState:UIControlStateNormal];
        self.toggleButton.tag = 222;
    }else{
        //selected state
        [self.toggleButton setTitle:@"More..." forState:UIControlStateNormal];
        self.toggleButton.tag = 111;
    }
}
// Reference to UIButton in Storyboard
@IBOutlet weak var toggleButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    // assumes you have two images in the bundle/project 
    // called normal.png and selected.png.
    let normalImage = UIImage(named: "normal.png")
    let selectedImage = UIImage(named: "selected.png")

    toggleButton.setImage(normalImage, for: .normal)
    toggleButton.setImage(selectedImage, for: .selected)
}
@IBAction func didPressButton(_ sender: Any) {
    
    // if the button was selected, then deselect it.
    // otherwise if it was not selected, then select it.
    toggleButton.isSelected = !toggleButton.isSelected

    if toggleButton.isSelected {
        print("I am selected.")

    } else {
        print("I am not selected.")
    }
}