Hi. I'm saving an NSInteger using NSUserDefaults, and it doesn't seem to be saving it. Every time I kill the app and go back in, it will go back to zero. The idea of it is that the user will click a button and numberOfTimesPressed (the NSInteger) will increase by 1. Here is how I save it:
Code:
-(IBAction)buttonPressed {
numberOfTimesPressedValue += 1;
NSUserDefaults *numberOfTimesPressedSaved = [NSUserDefaults standardUserDefaults];
[numberOfTimesPressedSaved setInteger:numberOfTimesPressedValue forKey:@"numberOfTimesPressed"];
label.text = [NSString stringWithFormat:@"%d", numberOfTimesPressedValue];
}
Here is how I retrieve it when the user kills the app then comes back:
Code:
-(void)viewDidLoad {
[super viewDidLoad];
NSUserDefaults *numberOfTimesPressedSaved = [NSUserDefaults standardUserDefaults];
numberOfTimesPressedValue = [numberOfTimesPressedSaved integerForKey:@"numberOfTimesPressed"];
label.text = [NSString stringWithFormat:@"%d", numberOfTimesPressedValue];
}
This viewDidLoad method is in the same .m file as the IBAction. Any ideas as to why it wouldn't be saving? Thanks in advance.