“On” state of a UISwitch

2019-08-24 13: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