如何使用UIButton作为切换button?

我正在尝试为表中的每个单元格创build一个切换button。 当按下时,它将改变图像,当再次按下它将再次改变图像 – 切换。

UIButton类中,我看不到selected状态。

我正在寻找一种方法来创build与UIButton的切换button,以便我可以更改每次点击状态。

这就是我现在使用rmqrubymotion做的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 

您可以轻松创build切换button,您只需要为各个状态设置相应的图像,然后可以使用selected属性在这些图像之间切换。

我做了一个纯粹的Objective-C代码来展示你如何做到这一点,但是你也可以在故事板或Xibs中设置图片,查看:

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

 // Don't forget to add an action handler to toggle the selected property [button addTarget:self action:@selector(buttonTouch:withEvent:) forControlEvents:UIControlEventTouchUpInside]; 

 // Now, in your button action handler, you can do something like this: - (void)buttonTouch:(UIButton *)aButton withEvent:(UIEvent *)event { button.selected = !button.selected; } 

我希望这可以帮助你。

Swift 4.0解决scheme(使用Storyboard)

首先,确保UIButton Type在属性检查器中设置为Custom

确保UIButton Type被设置为Custom

 // 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) } 

下面的屏幕截图演示了Storyboard中的toggleButtonTouch Up Inside Event中的引用,即:用户点击button,该button将触发下面的didPressButton

在这里输入图像说明

 @IBAction func didPressButton(_ sender: Any) { // if the button was selected, then deselect it. // otherwise if it was not selected, then select it. toggleButton.isSelected = !trackingButton.isSelected if toggleButton.isSelected { print("I am selected.") } else { print("I am not selected.") } } 

既然你的问题确实提到了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 

请注意,切换是通过查询button的实际状态来实现的。 如果你需要记住这个以备后用,你可能想把它保存在一个实例variables中。

在你的样式表中:

 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 

请注意使用image_selected 。 那么,这在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 

正如你所看到的,你的控制器代码保持清洁,button的初始设置被转移到样式表,并且你已经整齐地扩展了rmq来理解所select的状态。

我最简单的逻辑来切换button图像。 🙂

 (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]; } 

你可以用一个非常简单的方法做到这一点。 首先,在你的button的viewDidLoad设置标签中。 让我们说self.toggleButton.tag = 111 。 现在在你的button动作function中,你可以切换button,如:

 - (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; } } 

你可以像这样改变button的图像[self.toggleButton setImage://some image forState:UIControlStateNormal]; 这是我想的最简单的方法。 希望它会有所帮助。