moo
moo

Reputation: 45

How to make the save data appear when loading view controller?

The following code is saving data and loading, but it doesn't show the data when loading view controller.It just says label (Xcode default uilabel). I believe that I have to use synchronize ,but I am not too sure. May someone help me out.

Implementation file:

- (IBAction)minus
{
    mini = mini - 1;
    minus.text = [NSString stringWithFormat:@"%i", mini];
    [[NSUserDefaults standardUserDefaults] setInteger:mini forKey:@"M"];
}

- (void)viewDidLoad
{
    mini = [[NSUserDefaults standardUserDefaults] integerForKey:@"M"];
}

Header file:

IBOutlet UILabel *minus;

Upvotes: 0

Views: 122

Answers (2)

Manuel
Manuel

Reputation: 10303

Change:

[[NSUserDefaults standardUserDefaults]setInteger:mini forKey:@"M"];

Into:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:mini forKey:@"M"];
[prefs synchronize];

And it should work.

And i dont see you setting minus.text in viewdidload.

Upvotes: 1

El Tomato
El Tomato

Reputation: 6707

As H2CO3 says, you need to end the save process with

[prefs synchronize];

Why don't you open the iPhone Simulator folder to see if a plist file has been created inside Library > Preferences?

Upvotes: 0

Related Questions