xcode 如何使用 Swift 在 iOS 中更改 UIButton 的状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25944791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the state of an UIButton in iOS using Swift?
提问by eeschimosu
I have Objective-C working code for six buttons but I don't seem to find how to change the state of a button using Swift. So how do I make the following code work in Swift?
我有六个按钮的 Objective-C 工作代码,但我似乎没有找到如何使用 Swift 更改按钮的状态。那么如何让下面的代码在 Swift 中工作呢?
[self.button2 setBackgroundImage:[UIImage imageNamed:@"button2_selected.png"] forState: UIControlStateSelected];
button1.selected = NO;
button2.selected = !button2.selected;
button3.selected = NO;
button4.selected = NO;
button5.selected = NO;
button6.selected = NO;
回答by JNYJ
Check for this:
检查这个:
var var_button_ = UIButton()
var_button_.setBackgroundImage(UIImage(named: "button2_selected.png"), forState: UIControlState.Normal)
var_button_.selected = false
var_button_.selected = !var_button_.selected
//var_button_.selected = true
Thanks BY JNYJ
感谢 JNYJ
回答by Anit Kumar
// You need to use the true and false for bool value in swift
// 您需要在 swift 中为 bool 值使用 true 和 false
buttonOutletName.selected = true buttonOutletName.selected = false
buttonOutletName.selected = true buttonOutletName.selected = false
// Create IBAction and check button selected or not in swift language. You can change the button state.
// 创建 IBAction 并在 swift 语言中检查按钮是否被选中。您可以更改按钮状态。
@IBAction func favoriteButtonAction(sender: UIButton) {
// Save Data
sender.selected = !sender.selected;
if (sender.selected)
{
NSLog(" Not Selected");
sender.selected = false
self.setImage(UIImage(named: "checked_checkbox"), forState: .Normal)
}
else
{
NSLog(" Selected");
sender.selected = false
self.setImage(UIImage(named: "unchecked_checkbox"), forState: .Normal)
}
}
回答by ak_ninan
Create an outlet and an action for your button
为你的按钮创建一个插座和一个动作
Give 'button_selected' and 'button_not_selected' images for state configs Selected and Default respectively.
分别为状态配置 Selected 和 Default 提供 'button_selected' 和 'button_not_selected' 图像。
Then write the below code in the action,
然后在动作中写下下面的代码,
if button.isSelected {
button.isSelected = false
}
else {
button.isSelected = true
}
I think this is the simplest way
我认为这是最简单的方法
回答by redEyeProgrammer
//Method 1 //Create IBAction and check button selected or not...There is no need to set //sender.selected to true or false in the if/else statement.
//方法1 //创建IBAction并检查按钮是否选中...if/else语句中无需设置//sender.selected为true或false。
@IBAction func rememberMe(sender: UIButton) {
sender.selected = !sender.selected
if sender.selected {
self.setImage(UIImage(named: "RememberMe_Checkmark"), forState: .Normal)
} else {
self.setImage(UIImage(named: "No_Checkmark"), forState: .Normal)
}
}
//METHOD 2 With Custom Type from Storyboard
//方法 2 使用 Storyboard 中的自定义类型
enter image description hereClick on the Image Link to See Sample
在此处输入图像描述单击图像链接查看示例
From the Storyboard do the following
从故事板执行以下操作
1) Change the Button type to "Custom"
1)将按钮类型更改为“自定义”
2) Change the State Config to "Selected"
2)将状态配置更改为“已选择”
3) Set the Image to the Checkbox Icon with a checkmark asset
3)将图像设置为带有复选标记资产的复选框图标
4) Set the Background to the Checkbox Icon without a checkmark asset
4) 将背景设置为复选框图标,不带复选标记资产
//Create and IBAction from the Button and enter this single Line of Code
//从按钮创建和IBAction并输入这行代码
@IBAction func rememberMe(sender: UIButton) {
sender.selected = !sender.selected
}