Guille Pérez Guille Pérez - 26 days ago 9
iOS Question

If statements with UISwitch state in Swift

I was wondering how I can I write an if statement for the state of a switch. I've tried with this:


if switch1 == true{
label.text = "Light gray"
scrollView.backgroundColor = UIColor.lightGrayColor()
}


Before that I created this outlets:

@IBOutlet weak var switch1: UISwitch!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var label: UILabel!


But this isn't working. How can I do this?

Answer Source

Use the .on property. You don't need to compare against true as well this way:

if switch1.on {
   label.text = "Light gray"
   scrollView.backgroundColor = UIColor.lightGrayColor()
}