alexgrimaldi
alexgrimaldi

Reputation: 303

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?

Upvotes: 5

Views: 9578

Answers (3)

Nate Chandler
Nate Chandler

Reputation: 4553

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.

Upvotes: 16

Bruno Koga
Bruno Koga

Reputation: 3874

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];
}

Upvotes: 3

mmccomb
mmccomb

Reputation: 13807

Use the setOn:animated: method...

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

e.g.

[someSwitch setOn:NO animated:YES];

UISwitch Documentation

Upvotes: 2

Related Questions