gpt4 book ai didi

ios - UISwitch 无法快速正常工作

转载 作者:行者123 更新时间:2023-11-28 11:14:09 25 4
gpt4 key购买 nike

我在我的新 swift 项目中使用了一个 UISwitch 控件。 Storyboard的默认状态设置为 Off。我将其限制为 IBOutlet

@IBOutlet weak var SwitchRemFacility: UISwitch!

我还通过 Storyboard 中的值更改事件将其绑定(bind)到一个方法。

@IBAction func rememberFacilityClick(sender: AnyObject) {
print(SwitchRemFacility.on)
if SwitchRemFacility.on {
SwitchRemFacility.setOn(false, animated: true)
} else {
SwitchRemFacility.setOn(true, animated: true)
}
}

问题是 SwitchRemFacility.on 在这里总是返回 true。我已通过断点进行检查,并将其打印到控制台。任何帮助将不胜感激。

提前致谢。

最佳答案

当值已经改变时,您正在测试它。开关会自行完成开关工作。

如果你想手动完成,你可以这样做:

@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
}
}

关于ios - UISwitch 无法快速正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32942166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com