How to test the state of a UISwitch

- by

We can use the isOn property of your switch to do this:

if (flickSwitch isOn) {
 statusLabel.text = @"The Switch is ON";
 } else {
 statusLabel.text = @"The Switch is OFF";
 }

Alternatively we can use the square-bracket notation like so:

if ([flickSwitch.isOn]) {
 statusLabel.text = @"The Switch is ON";
 } else {
 statusLabel.text = @"The Switch is OFF";
 }



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

2 thoughts on “How to test the state of a UISwitch”

  1. You know what it’s like… a few months later this doesn’t work anymore, or it’s the other way round. I H-A-T-E Xcode and this Objective-C 5H1T so very very very much.

    That aside, here’s another solution which works (today that is):

    if (flickSwitch.on == YES) {
      // jump off a bridge
      } else {
      // take an overdose
      }
    
  2. There is another way by checking if the sender’s property isOn or not, like so:

        if ([sender isOn]) {
            // do something good
        } else {
            // do something else
        }
    

    This is useful if you don’t want to define the switch as a property. All you need to do is connect it to an action which will check the above.

Leave a Reply to JayCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.