More newbie questions... I have several bool functions that if selected change the sate of a button. Here is some of the code:
if (checkboxSelected == 0){
[checkboxButton setSelected:YES];
} else {
checkboxSelected = 1;
[checkboxButton setSelected:NO];
}
}
I want a user to be able to save the state of the bool and the corresponding state of the button that is affected by the bool. I am also using the NSUserDefaults to save information from textFields elsewhere in my app using this type of code:
//Saving the default settings
NSUserDefaults *Prefs = [NSUserDefaults standardUserDefaults];
[Prefs setObject:inputField.text forKey:@"savedData"];
defaultSettings.text = @"Saved!";
//Loading the default settings
NSUserDefaults *Prefs = [NSUserDefaults standardUserDefaults];
NSString *Defaults = [Prefs stringForKey:@"savedData"];
if(Defaults == nil) {
defaultSettings.text = @"";
} else {
inputField.text = [[NSString alloc] initWithFormat:@"%@", Defaults];
}
That works great, but I have been unable to figure out the code for the bool functions. Can anyone point me to a tutorial or help clarify the code necessary? I've been Apple's dev center but now am even more confused.
So If I'm reading your question right you want to know how to pull the BOOL value from the NSUserDefaults. Since NSUserDefaults is nothing more than a dictionary these should work...
NSUserDefaults *Prefs = [NSUserDefaults standardUserDefaults];
// This will set it the defaults value
[prefs setBool:YES forKey:@"myCheckBoxValue"];
// This will get the BOOL value and set the checkboxButton BOOL
[checkboxButton setSelected:[prefs boolForKey:@"myCheckBoxValue"]];