“On” state of a UISwitch

2019-08-24 13:29发布

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?

3条回答
倾城 Initia
2楼-- · 2019-08-24 13:42

To disable switch :

switch.isEnabled = false

To change the switch state to off:

switch.isOn = false
查看更多
Viruses.
3楼-- · 2019-08-24 13:50

To disable switch :

Objective C

[switch setEnabled:NO];

Swift

switch.isEnabled = false
查看更多
干净又极端
4楼-- · 2019-08-24 14:03

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.

查看更多
登录 后发表回答