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

前端 未结 7 1392
深忆病人
深忆病人 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]

    0 讨论(0)
  • 2020-12-24 12:40

    Drag and drop UISwitch from Object Library-

    @IBOutlet-

    @IBOutlet weak var myswitch: UISwitch!
    

    @IBAction-

    @IBAction func onAllAccessory(sender: UISwitch) {
    
            if myswitch.on == true{
                onCall()
            }
            if myswitch.on == false{
                offCall()
            }
        }
    

    Your function-

    func onCall(){
            print("On is calling")
        }
        func offCall(){
            print("Off is calling")
        }
    
    0 讨论(0)
  • 2020-12-24 12:44

    If you are trying to change the switch you can use

    mySwitch.on = true
    

    to set it to "on" or

    mySwitch.off == false
    

    to set it to "off"

    Or if you want to get the value you can use

    let myBool = mySwitch.on
    

    If the switch is powered on the value will be true, otherwise false.

    You can also make an action called valueChanged (to get or update the value whenever the switch value is changed)

    @IBAction func valueChanged(sender: AnyObject) {
        // some code
    }
    

    You can do this by select the dualView... view and ctrl drag from your switch to you view controller and choose action, and value changed if that's not already selected.

    0 讨论(0)
  • 2020-12-24 12:46

    In Swift 3, is chance.

    if mySwitch.on
    

    to

    if mySwitch.isOn
    

    myprint

    0 讨论(0)
  • 2020-12-24 12:49

    Add the UISwitch Reference into ViewController.swift file.

    @IBOutlet var mySwitch: UISwitch 
    @IBOutlet var switchState: UILabel
    

    then add the target event into the viewdidload method like below

    mySwitch.addTarget(self, action: #selector(ViewController.switchIsChanged(_:)), forControlEvents: UIControlEvents.ValueChanged)
    

    When the switch is flipped the UIControlEventValueChanged event is triggered and the stateChanged method will be called.

    func switchIsChanged(mySwitch: UISwitch) {
        if mySwitch.on {
            switchState.text = "UISwitch is ON"
        } else {
            switchState.text = "UISwitch is OFF"
        }
    }
    

    Swift 3.0

    func switchIsChanged(mySwitch: UISwitch) {
        if mySwitch.isOn {
            switchState.text = "UISwitch is ON"
        } else {
            switchState.text = "UISwitch is OFF"
        }
    }
    

    Swift 4.0

    @objc func switchIsChanged(mySwitch: UISwitch) {
        if mySwitch.isOn {
            switchState.text = "UISwitch is ON"
        } else {
            switchState.text = "UISwitch is OFF"
        }
    }
    

    find brief tutorial in http://sourcefreeze.com/uiswitch-tutorial-using-swift-in-ios8/

    0 讨论(0)
  • 2020-12-24 12:57

    For swift 3

    @IBAction func switchValueChanged(_ sender: UISwitch) {
            print(sender.isOn)
    }
    
    0 讨论(0)
提交回复
热议问题