Ant*_*ony 21 iphone uibutton ios rubymotion rmq
我正在尝试为表格中的每个单元格创建一个切换按钮.按下时,它将改变图像,再次按下时,它将再次改变图像 - 切换.
在UIButton课堂上,我没有看到一个selected州.
我正在寻找一种方法来创建一个使用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
Run Code Online (Sandbox Code Playgroud)
Jan*_*sio 40
您可以轻松创建切换按钮,只需为各个状态设置相应的图像,之后,您可以使用selected属性在这些图像之间切换.
我制作了一个纯粹的Objective-c代码来展示你如何做到这一点,但你也可以在Storyboards ou Xibs中设置图像,请查看:
// First, set the images for normal state and selected state
[button setImage:normalImage forState:UIControlStateNormal];
[button setImage:selectedImage forState:UIControlStateSelected];
Run Code Online (Sandbox Code Playgroud)
// Don't forget to add an action handler to toggle the selected property
[button addTarget:self action:@selector(buttonTouch:withEvent:) forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)
// Now, in your button action handler, you can do something like this:
- (void)buttonTouch:(UIButton *)aButton withEvent:(UIEvent *)event
{
aButton.selected = !aButton.selected;
}
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮到你.
Swift 4.0 解决方案(使用故事板)
首先,确保在属性检查器中将UIButton类型设置为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)
}
Run Code Online (Sandbox Code Playgroud)
下面的屏幕截图演示toggleButton了 Storyboard 和Touch Up InsideEvent 中的引用,即:用户点击按钮,在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 = !toggleButton.isSelected
if toggleButton.isSelected {
print("I am selected.")
} else {
print("I am not selected.")
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20978 次 |
| 最近记录: |