“On” state of a UISwitch

北慕城南 提交于 2019-12-11 14:18:57

问题


I have a switch and I want to have something like this:

override func viewDidLoad() {
    super.viewDidLoad()
    if name.text == "Jack"{
        switch.on = false
    }
}

But it's not working! How can I change the state of a switch when the view controller is loading?


回答1:


UISwitch has a property isOn to turn the switch on and off. Also don't use the name switch since this is a reserved keyword in Swift.




回答2:


To disable switch :

switch.isEnabled = false

To change the switch state to off:

switch.isOn = false



回答3:


To disable switch :

Objective C

[switch setEnabled:NO];

Swift

switch.isEnabled = false


来源:https://stackoverflow.com/questions/46899032/on-state-of-a-uiswitch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!