1

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

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
pixelbitlabs
  • 1,934
  • 6
  • 36
  • 65
  • What specifically is not working? What output are you getting? – PengOne Oct 11 '11 at 06:15
  • Also, what output are you expecting? Limit the example to one of these 6 instances (since all appear to be identical). – PengOne Oct 11 '11 at 06:18
  • Basically a DatePicker gives a day of the month and puts it into a string (this is all fine, it works perfectly) and then I need to save that into a label, which it does, but when I reload the app - it's just gone back to the default number - which happens to be "28" rather than my specified DatePicker number... – pixelbitlabs Oct 11 '11 at 06:49
  • Also, in my NSLog, it does actually load the text (like so: http://pastie.org/2675529) but I think the problem, is getting it into the label :( – pixelbitlabs Oct 11 '11 at 07:10
  • Might sound ridiculous, can we try like this [CalLabel1 setText:callbltxt1]; and see if that works? – Saran Oct 11 '11 at 08:35
  • Tried that, doesn't work unfortunately. I think it's all there, it just needs to be put into that label! :( – pixelbitlabs Oct 11 '11 at 15:21

2 Answers2

0

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

Also, your code has unused variables.

vocaro
  • 2,779
  • 2
  • 23
  • 16
0

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.

Sanchit Paurush
  • 6,114
  • 17
  • 68
  • 107
  • but what would I need to do with this? :) – pixelbitlabs Oct 11 '11 at 06:47
  • Ok Do one thing. First you run the application in simulator. Then go to the settings and then select your application's setting. and edit all the fields there. Then stop the application and run it again. I am sure it will work then. – Sanchit Paurush Oct 11 '11 at 07:09
  • No, I'm not using the settings bundle, you misunderstood what my app does. Basically the user chooses a date and then that date is entered into a UILabel (this all works fine) and then it saves and loads using NSUserDefaults - this is what isn't working... – pixelbitlabs Oct 11 '11 at 07:11
  • Ok. So you first take date from user then save it in settings.bundle or somewhere else. coz NSUserDefaults saves in settings.bundle in root.plist file. right? – Sanchit Paurush Oct 11 '11 at 07:12
  • Not sure where it saves it, but does that really matter? All I need to do is save and load - it doesn't really matter where it is saved :-) – pixelbitlabs Oct 11 '11 at 07:16
  • ok then this link will help you exactly http://www.icodeblog.com/2008/10/03/iphone-programming-tutorial-savingretrieving-data-using-nsuserdefaults/ – Sanchit Paurush Oct 11 '11 at 07:17
  • But my code is correct... I need to know why the code I am using is not working as it's actually very similar to the code on that blog :) – pixelbitlabs Oct 11 '11 at 07:20
  • You need to check the complete procedure of NSUserDefaults. Only writing code doesn't solve the problem. You need to create root.plist file also and save entries there. First check, how to save the data using NSUserDefault. Because everything matters equally. – Sanchit Paurush Oct 11 '11 at 07:27
  • You may try this one last option http://stackoverflow.com/questions/471830/why-nsuserdefaults-failed-to-save-nsmutabledictionary-in-iphone-sdk – Sanchit Paurush Oct 11 '11 at 07:29
  • But I'm not getting that error? I also don't think that answer will help. I'm sorry but I don't really think you entirely know what the issue is... – pixelbitlabs Oct 11 '11 at 07:32