How can I set the default value of my UISwitch object to Off? I am probably missing something obvious?
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":
† 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
Once the Utilities Area appears with the Inspector Pane above the Library Pane, and select the Attributes Inspector item from the Inspector selector bar
All of this terminology comes from this image from Apple's Xcode documentation.
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];
}
Use the setOn:animated: method...
- (void)setOn:(BOOL)on animated:(BOOL)animated
e.g.
[someSwitch setOn:NO animated:YES];
UISwitch Documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With