views:

336

answers:

2

Hi, I'm creating a simple app to simply save a person's name and a number associated with this person. I've created a class for person like this:

@interface Person : NSObject {
    NSString *name;
    NSInteger serialNumber;
}

In the mainViewController, I've created an input text field to enter the name and then automatically assign a number to the person, also 2 UILabel to show the inputted name and the serialNumber.

However, after quitting the app and re-open, all labels cleared. What I'm trying to do is to keep these variables as they were when quitting the app and display in the labels. Hence, I created 2 variables in the app delegate and tried to use applicationWillTerminate: in the app delegate to save . In the mainViewController I've inserted these codes after each time I typed in a new name:

NameAppDelegate *appDelegate = (NameAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.lastPerson.name = aPerson.name;
//currentPerson is an instance variable of the app delegate
appDelegate.lastPerson.serialNumber = aPerson.serialNumber;
NSLog(@"aPerson.serialNumber = %i", aPerson.serialNumber);
NSLog(@"appDelegate.lastPerson.serialNumber = %i", appDelegate.lastPerson.serialNumber);

However, each time I typed a new name, the console showed a change in aPerson.serialNumber (which is in mainViewController) BUT not the appDelegate.lastPerson.serialNumber (which is always equal to zero).

Is there anything wrong with the codes above? Thanks for any help.

+2  A: 

You want to save to NSUserDefaults (documentation). Look at the UserDefaults example in the documentation.

Also check out this question:

http://stackoverflow.com/questions/2076816/how-to-register-user-defaults-using-nsuserdefaults-without-overwriting-existing-v

mga
+2  A: 

Easy to do with NSUserDefaults, here's some code to get you started:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    appDelegate.lastPerson.name = [defaults stringForKey:@"Name"];
    appDelegate.lastPerson.serial = [defaults stringForKey:@"Serial"];

    [window addSubview:[flipViewController view]];
    [window makeKeyAndVisible];
}

-(void)applicationWillTerminate:(UIApplication *)application {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setString:appDelegate.lastPerson.name forKey:@"Name"];
    [defaults setString:appDelegate.lastPerson.serial forKey:@"Serial"];
    [defaults synchronize];     
}

You can check if either of the strings are nil after loading which will indicate no stored value and you should then ask the user for input.

David Kanarek
Thanks David. But should I add these codes in the appDeleagate.m or in the mainViewController.m? (I do most of my program in mainViewController.m)Also, I have a problem that appDelegate.lastPerson.name and appDelegate.lastPerson.serial do not change as I expected. They always show a (null) value even after assigning some content to them.
Anthony Chan
In the AppDelegate. DidFinishLaunching should already be there, so you don't need to copy the whole thing, just the essential bits. You can copy in WillTerminate and it should work, though you'll have to change the appDelegate variable as it's obviously not setup properly.
David Kanarek
OK, I got that. But I have another problem here. The appDelegate.lastPerson.name and appDelegate.lastPerson.serial do not change as I expected. So eventually I can't save the defaults settings even I have these codes. Do you have any idea why I can't update them with the above codes I quoted?
Anthony Chan
Did you change the code from appDelegate.lastPerson.name to lastPerson.name which should work? Are you sure lastPerson is set up properly? It has to be done before you can set any of its ivars.
David Kanarek
I'm quite sure I've set the lastPerson in appDelegate when the app launch. By the way, I'm updating lastPerson.name through mainViewController.m by using appDelegate.lastPerson.name = aPerson.name (aPerson is a variable in mainViewController.m) Is this causing the problem?
Anthony Chan
It shouldn't be, but it's possible that you haven't set up aPerson right and that's why it isn't working. Can you set a breakpoint where you write to lastPerson and inspect aPerson to make sure the values are correct?
David Kanarek
Thanks again David. Now everything works. It's my stupid mistake that I should remove the app from iPhone simulator so that any data file should be removed before build and run a new version. Anyway, thanks very much for your help!
Anthony Chan
Ah yes, everyone deals with it once. Mind accepting my answer if you used it?
David Kanarek