Set an Interface Builder created element's state programatically

Posted by mvexel on Stack Overflow See other posts from Stack Overflow or by mvexel
Published on 2010-04-20T09:59:43Z Indexed on 2010/04/20 10:03 UTC
Read the original article Hit count: 407

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.

© Stack Overflow or respective owner

Related posts about nsuserdefaults

Related posts about uiswitch