Set an Interface Builder created element's state programatically
- by mvexel
I have a couple of UISwitch elements in a view controller that is presented modally in my iPhone app. I set up the view in IB. I want these UISwitch elements to reflect the current values in my [NSUserDefaults standardUserDefaults] where I store the appropriate BOOLs.
I thought this would do the trick setting the switches to the right state, but no:
-(void)viewWillAppear:(BOOL)animated {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[backgroundSwitch setOn:[defaults boolForKey:kTrackInBackgroundKey] animated:NO];
[batterySaveSwitch setOn:[defaults boolForKey:kBatterySaveKey] animated:NO];
}
The backgroundSwitch and batterySaveSwitch are declared as properties for the view controller class. They are not initialized; that does not seem to make a difference. I did check the values coming out of the NSUserDefaults dictionary. The method is being called at the right time.