如果在Swift中具有UISwitch状态的语句

时间:2015-07-06 16:43:03

标签: ios swift

我想知道如何为交换机状态编写if语句。我试过这个:

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

在此之前我创建了这个网点:

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

但这不起作用。我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

使用.on属性。您也不需要以这种方式与true进行比较:

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

答案 1 :(得分:0)

Swift 4解决方案

.on属性已重命名为.isOn,因此您的代码应该看起来像这样:

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