I want to save the swtich state and load it when the program restarted.
[[NSUserDefaults standardUserDefaults] setBool:switchControl.on forKey:@"switch"];
[[NSUserDefaults standardUserDefaults] synchronize];
This is the saving part
BOOL test= [[NSUserDefaults standardUserDefaults] boolForKey:@"switch"];
NSLog(@"%@",test?@"YES":@"NO");
if(test == YES)
[switchControl setOn:YES animated:YES];
else
[switchControl setOn:NO animated:YES];
This is the part that is need to be set the switch to its value.I did this in viewdidload method because when i close the application and restarted it i want the state of the switch set the saved part .
But it is still showing the default part can u help me to set it?
Did you create the switch as an IBOutlet with properties? If so, I think your problem is that you dont refer to your
switchControlasself.switchControl.Then your correct saving statement will become
I’d move the part that sets the switch into
viewWillAppearand do it like this:I took out an unnecessary if-statement for you, too.