如何使用UIButton作为切换按钮? [英] How to use UIButton as Toggle Button?

查看:187
本文介绍了如何使用UIButton作为切换按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a toggle button for each cell in my table. When pressed, it will change the image and when pressed again it will change the image again -- Toggle.

UIButton 类中,我没有看到选择 状态。

In the UIButton class I don't see a selected state.

我正在寻找一种使用UIButton创建切换按钮的方法,这样我就可以在每次点击时更改状态。

I'm looking for a way to create a toggle button with UIButton so that I can change the state on each click.

我现在正在使用 rmq ruby​​motion 进行此操作

This is how I'm doing it in rubymotion right now using 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


推荐答案

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

You can create toggle button easily, you just need to set respective images for respective states, after that, you can use the selected property to toggle between these images.

我制作了一个纯粹的Objective-c代码来展示你如何做到这一点,但你也可以在Storyboards ou Xibs中设置图像,结账:

I made a pure objective-c code to show how you can do that, but you can set the images anyway in Storyboards ou Xibs too, check out:

// 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
{
  aButton.selected = !aButton.selected;
}

我希望这可以帮到你。

这篇关于如何使用UIButton作为切换按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆