StevoHN
StevoHN

Reputation: 481

Can't switch state of UISwitch

I have to UISwitches in my application. At launch, they're both set to off and the second switch is disabled and should only be enabled when the first switch is aswell, meaning:

@IBAction func switchOneToggled(sender: UISwitch) {
    if switchOne.on {
        switchTwo.enabled = true
    }
    else {
        switchTWo.enabled = false
    }
}

My problem is, that when I enable switchOne, switchTwo gets enabled, but I can't toggle switchTwo's on/off state by touching it.

Upvotes: 0

Views: 3293

Answers (3)

Kode
Kode

Reputation: 620

Try resetting the switches. I had a similar problem and just deleted the switch and replaced it, and giving it the same connections that is had before. It must be a bug inside Xcode, since it should be able to turn on and off without any code.

Upvotes: 1

Vakas
Vakas

Reputation: 6472

The problem is that your switchOneToggled is called when the Switch is tapped and the state of the switch is changed. You should check the state of Switch 1 and enable your Switch 2 in your viewDidLoad.

Upvotes: 0

André Slotta
André Slotta

Reputation: 14040

@IBAction func switchOneToggled(sender: UISwitch) {
  switchTwo.enabled = switchOne.on
}

do you maybe have userinteractionenabled set to false for switchTwo?

Upvotes: 0

Related Questions