我在同一 View 上有多个 UISwitch。在我的表单上,我需要使用 UISwitch 像单选按钮(网络)。不能同时打开超过一个 UISwitch,我只需要一个。
如果一个 UISwitch 打开,我想检查其他 UISwitch,如果有任何 UISwitch 处于打开模式,需要将其关闭。可能吗?
我可以在按钮操作中检查它,但是,我想实时更改 UISwitch 状态。
解决后更新:
这是我的代码:
@IBOutlet weak var firstSwitch: UISwitch!
@IBOutlet weak var secondSwitch: UISwitch!
@IBAction func firstSwitchAction(sender: AnyObject) {
if self.secondSwitch.on == true {
self.secondSwitch.setOn(false, animated: true)
}
}
@IBAction func secondSwitchAction(sender: AnyObject) {
if self.firstSwitch.on == true {
self.firstSwitch.setOn(false, animated: true)
}
}
最佳答案
您需要为您的开关创建 socket ,您可以使用.setOn方法更改它们
一个简单的例子
@IBOutlet weak var mySwitch: UISwitch!
@IBOutlet weak var myOtherSwitch: UISwitch!
@IBAction func mySwitchTouched(sender: UISwitch) {
if sender.on == true {
myOtherSwitch.setOn(false, animated: true)
}
}
关于ios - Swift:通过查看其他 UISwitch 的状态来检查 UISwitch 的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31678509/