Objective-c – ObjectiveC UISwitch set default to OFF

objective-cuiswitch

How can I set the default value of my UISwitch object to Off? I am probably missing something obvious?

Best Solution

As mmccomb and Bruno Koga mentioned, this can be done programmatically. In this case, though--assuming that your UISwitch is getting unpacked from a nib--it may be more convenient to simply configure your UISwitch in Interface Builder.

To do this, open the xib that your switch is in, select your switch and open the Attributes Inspector (†) and within the "Switch" section, change the value of "State" from "On" to "Off":

Change the UISwitch's state from On to Off.


† To open the Attributes inspector, first depress the right-end item ("Utilities") in the "View" segmented control at the far right of Xcode's toolbar

Depress the Utilites item in the View segmented control.

Once the Utilities Area appears with the Inspector Pane above the Library Pane, and select the Attributes Inspector item from the Inspector selector bar

Select the Attributes Inspector item from the Inspector selector bar.

All of this terminology comes from this image from Apple's Xcode documentation.

Xcode 4 workspace.