Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ObjectiveC UISwitch set default to OFF

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

like image 360
alexgrimaldi Avatar asked Jun 01 '13 10:06

alexgrimaldi


3 Answers

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.

like image 99
Nate Chandler Avatar answered Nov 14 '22 05:11

Nate Chandler


Assuming tha you have a reference for your UISwitch, you could simply override the -viewDidLoad method on your controller:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [yourSwitch setOn:NO animated:NO];
}
like image 40
Bruno Koga Avatar answered Nov 14 '22 05:11

Bruno Koga


Use the setOn:animated: method...

- (void)setOn:(BOOL)on animated:(BOOL)animated

e.g.

[someSwitch setOn:NO animated:YES];

UISwitch Documentation

like image 2
mmccomb Avatar answered Nov 14 '22 05:11

mmccomb