cory ginsberg
cory ginsberg

Reputation: 2897

My NSUserDefaults code does not work

When I try to use NSUserDefaults on two different screens, the NSUserDefaults does not apply and there is not an error that comes up. Basically, I want the next level to be available only after the first one is completed.

Here is my code:

View1:

- (void)viewDidLoad {      
    [super viewDidLoad];
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSInteger level1Complete = [prefs integerForKey:@"levelComplete"];

    if (level1Complete == 11) {
        button2.hidden = NO;
    }
}

View2:

if (number11 == 5) {

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setInteger:11 forKey:@"levelComplete"];

    [prefs synchronize];
}

Please note: the game play itself is view2 and the level select is view1.

Upvotes: 0

Views: 122

Answers (1)

Nishith Shah
Nishith Shah

Reputation: 523

[[NSUserDefaults standardUserDefaults] setValue:@"11" forKey:@"levelComplete"];

if you can save it as a value and when you fetch it then convert it into integer.

All The Best!!

Upvotes: 2

Related Questions