(Problem solved now, see answers below)
Hello, in my app I am trying to create a points counter. That increases a number by 10 every time a button is pressed.
I successfully managed to get my app to increase a value (0) by 1 every time a button is pressed, and to remember the latest value and display it next time the app starts. But when you press the button that's suppose to increase the points value by 1 after the app restarts the number starts increasing from 0 not the remembered value.
How would I get the points to increase by 10 and remember the latest value and display (and count from it) after the application is exited and started again ?
Here is my current code:
Code for the button to increase the value by 1:
- (IBAction)startClick:(id)sender{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:count forKey:@"greeting"];
NSString *numValue = [[NSString alloc] initWithFormat:@"%d", count++];
counter.text = numValue;
viewDidLoad method:
- (void)viewDidLoad {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *greetName = [prefs stringForKey:@"greeting"];
NSInteger count = [prefs integerForKey:@"greeting"];
counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];
if(count == 0) {
counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];
} else {
counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];
}