开发者

Why is my UILabel not saving/loading with NSUserDefaults?

Here is my code to save and load. I did an NSLog, and it shows the text which should be saved/loaded but it isn't, and just shows the default text on launch in the UILabel.

UPDATE:

Also, in my NSLog, it does actually load the text but I think the problem is getting it into the label.

To save:

NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];

NSString* CalLabel1Text = CalLabel1.text;
[defaults setObject:CalLabel1Text forKey:@"CalLabel1TextKey"];
NSString* callbltxt1 = [defaults objectForKey:@"CalLabel1TextKey"];
CalLabel1.text = callbltxt1;
NSLog(@"Saved Cal Label 1: %@", callbltxt1);

NSString* CalLabel2Text = CalLabel2.text;
[defaults setObject:CalLabel2Text forKey:@"CalLabel2TextKey"];
NSString* callbltxt2 = [defaults objectForKey:@"CalLabel2TextKey"];
CalLabel2.text = callbltxt2;
NSLog(@"Saved Cal Label 2: %@", callbltxt2);

NSString* CalLabel3Text = CalLabel3.text;
[defaults setObject:CalLabel3Text forKey:@"CalLabel3TextKey"];
NSString* callbltxt3 = [defaults objectForKey:@"CalLabel3TextKey"];
CalLabel3.text = callbltxt3;
NSLog(@"Saved Cal Label 3: %@", callbltx开发者_运维技巧t3);

NSString* CalLabel4Text = CalLabel4.text;
[defaults setObject:CalLabel4Text forKey:@"CalLabel4TextKey"];
NSString* callbltxt4 = [defaults objectForKey:@"CalLabel4TextKey"];
CalLabel4.text = callbltxt4;
NSLog(@"Saved Cal Label 4: %@", callbltxt4);

NSString* CalLabel5Text = CalLabel5.text;
[defaults setObject:CalLabel5Text forKey:@"CalLabel5TextKey"];
NSString* callbltxt5 = [defaults objectForKey:@"CalLabel5TextKey"];
CalLabel5.text = callbltxt5;
NSLog(@"Saved Cal Label 5: %@", callbltxt5);

NSString* CalLabel6Text = CalLabel6.text;
[defaults setObject:CalLabel6Text forKey:@"CalLabel6TextKey"];
NSString* callbltxt6 = [defaults objectForKey:@"CalLabel6TextKey"];
CalLabel6.text = callbltxt6;
NSLog(@"Saved Cal Label 6: %@", callbltxt6);

[defaults synchronize];

To load:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString* CalLabel1Text = CalLabel1.text;
NSString* callbltxt1 = [defaults objectForKey:@"CalLabel1TextKey"];
CalLabel1.text = callbltxt1;
NSLog(@"Loaded Cal Label 1: %@", callbltxt1);

NSString* CalLabel2Text = CalLabel2.text;
NSString* callbltxt2 = [defaults objectForKey:@"CalLabel2TextKey"];
CalLabel2.text = callbltxt2;
NSLog(@"Loaded Cal Label 2: %@", callbltxt2);

NSString* CalLabel3Text = CalLabel3.text;
NSString* callbltxt3 = [defaults objectForKey:@"CalLabel3TextKey"];
CalLabel1.text = callbltxt3;
NSLog(@"Loaded Cal Label 3: %@", callbltxt3);

NSString* CalLabel4Text = CalLabel4.text;
NSString* callbltxt4 = [defaults objectForKey:@"CalLabel4TextKey"];
CalLabel1.text = callbltxt4;
NSLog(@"Loaded Cal Label 4: %@", callbltxt4);

NSString* CalLabel5Text = CalLabel5.text;
NSString* callbltxt5 = [defaults objectForKey:@"CalLabel5TextKey"];
CalLabel5.text = callbltxt5;
NSLog(@"Loaded Cal Label 5: %@", callbltxt5);

NSString* CalLabel6Text = CalLabel6.text;
NSString* callbltxt6 = [defaults objectForKey:@"CalLabel6TextKey"];
CalLabel6.text = callbltxt6;
NSLog(@"Loaded Cal Label 6: %@", callbltxt6);

[defaults synchronize];

Why is this not working for me? I put the "save" code onto a button action and the "load" code in viewDidLoad.


I bet you forgot to wire up the referencing outlets in Interface Builder.

Also, your code has unused variables.


Check your settings.bundle file and its Root.plist. There might be some data problem or the objectForKey problem. Check this link

Hope it will help you out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜