UISwitch 在 swift 中无法正常工作
UISwitch not working properly in swift
我在我的新 swift 项目中使用了一个 UISwitch 控件。故事板的默认状态设置为 Off
。我将其限制为 IBOutlet
@IBOutlet weak var SwitchRemFacility: UISwitch!
我还通过情节提要中的值更改事件将其绑定到一个方法。
@IBAction func rememberFacilityClick(sender: AnyObject) {
print(SwitchRemFacility.on)
if SwitchRemFacility.on {
SwitchRemFacility.setOn(false, animated: true)
} else {
SwitchRemFacility.setOn(true, animated: true)
}
}
问题是 SwitchRemFacility.on
returns 在这里总是正确的。我已通过断点进行检查,并将其打印到控制台。任何帮助将不胜感激。
提前致谢。
当值已经改变时,您正在测试它。开关会自行完成开关工作。
如果您想手动完成,您可以这样做:
@IBAction func switchChangeVal(sender: AnyObject) {
let swh : UISwitch = sender as! UISwitch
if(swh.on){
swh.setOn(true, animated: true)//But it will already do it.
}
else{
swh.setOn(false, animated: true)
}
}
其实不需要代码。但是如果你想在开启或关闭状态下进行调节,那么你必须编写代码。
例如
@IBAction func switchChangeVal(sender: AnyObject) {
let swh : UISwitch = sender as! UISwitch
if(swh.on){
//Do the code for when switch is on
}
else{
//Do the code for when switch is off
}
}
我在我的新 swift 项目中使用了一个 UISwitch 控件。故事板的默认状态设置为 Off
。我将其限制为 IBOutlet
@IBOutlet weak var SwitchRemFacility: UISwitch!
我还通过情节提要中的值更改事件将其绑定到一个方法。
@IBAction func rememberFacilityClick(sender: AnyObject) {
print(SwitchRemFacility.on)
if SwitchRemFacility.on {
SwitchRemFacility.setOn(false, animated: true)
} else {
SwitchRemFacility.setOn(true, animated: true)
}
}
问题是 SwitchRemFacility.on
returns 在这里总是正确的。我已通过断点进行检查,并将其打印到控制台。任何帮助将不胜感激。
提前致谢。
当值已经改变时,您正在测试它。开关会自行完成开关工作。
如果您想手动完成,您可以这样做:
@IBAction func switchChangeVal(sender: AnyObject) {
let swh : UISwitch = sender as! UISwitch
if(swh.on){
swh.setOn(true, animated: true)//But it will already do it.
}
else{
swh.setOn(false, animated: true)
}
}
其实不需要代码。但是如果你想在开启或关闭状态下进行调节,那么你必须编写代码。
例如
@IBAction func switchChangeVal(sender: AnyObject) {
let swh : UISwitch = sender as! UISwitch
if(swh.on){
//Do the code for when switch is on
}
else{
//Do the code for when switch is off
}
}