How to link a boolean value to the on/off state of a UISwitch?

前端 未结 7 1525
深忆病人
深忆病人 2020-12-24 12:11

I have a UISwitch that I want to control a boolean value in a function I wrote. I looked in the UISwitch Type Reference and it listed the property

7条回答
  •  没有蜡笔的小新
    2020-12-24 12:38

    Succinctness, even parsimony, is important in coding style. Try this:

    @IBAction func switchValueChanged (sender: UISwitch) {
      advice.isInProduction = sender.on
      print ("It's \(advice.isInProduction)!")
    }
    

    In your original code, you likely crashed because acsessabilitySwitch or advice are unbound (have values of nil).

    [Updated - replaced println with print]

提交回复
热议问题