I'm trying to pass a variable (probably going to end up being a bool or an int, I just just want to indicate the state of a UISwitch) from one view to another. I've looked into this and it seems like the best way to do this is NSUserDefaults. However every time I try the solutions it seems to crash the app. Here's what I've done in my .m file:
Right-click-dragged the switch to the space in between @interface and @end and created an outlet
@property (weak, nonatomic) IBOutlet UISwitch *officer1;
Right-click-dragged the switch to the space in between @implementation and @end and created an action
- (IBAction)officer1Flipped:(id)sender {
[[NSUserDefaults standardUserDefaults] setObject:_officer1 forKey:@"officer1"];
}
But now it crashes whenever I flip that switch. Any idea what I'm doing wrong? Did I forget to include anything?
Instead of saving the whole UISwitch object to the user defaults, save the current on state (BOOL) officer1.isOn
- (IBAction)officer1Flipped:(id)sender {
[[NSUserDefaults standardUserDefaults] setBool:_officer1.isOn forKey:@"officer1"];
}
Then to read that value, call
[[NSUserDefaults standardUserDefaults] boolForKey:@"officer1"];