为什么我的UILabel没有使用NSUserDefaults保存/加载?

时间:2011-10-11 06:12:09

标签: iphone objective-c ios xcode nsuserdefaults

这是我保存和加载的代码。我做了一个NSLog,它显示了应该保存/加载的文本,但它不是,只显示UILabel中启动时的默认文本。

更新:

此外,在我的NSLog中,它确实load the text,但我认为问题在于将其纳入标签。

保存:

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: %@", callbltxt3);

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];

加载:

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];

为什么这不适合我?我将“保存”代码放在按钮操作上,并将“加载”代码放在viewDidLoad

2 个答案:

答案 0 :(得分:0)

我敢打赌你忘了在Interface Builder中连接参考插座。

此外,您的代码还有未使用的变量。

答案 1 :(得分:0)

检查您的settings.bundle文件及其Root.plist。可能存在一些数据问题或objectForKey问题。请检查此link

希望它会帮助你。