1

i created a simple project, with a "+" and "-" buttons that increase or decrease a number inside a label.

I wanted to test the registerDefaults method, but if i click on the "home" button, and come back on the project from the simulator (with my app's icon), the number is still the same on screen.

How can i test the user settings with this example? or is it something with the simulator, or would it work if i use the device and wait for the memory warning method to automatically refresh the project, and then it would work?

here's a bit of my code :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    NSNumber *testValue = [[NSUserDefaults standardUserDefaults] objectForKey:@"kRestore"];
    if (testValue){
        NSLog(@"already");
        viewController.myNum = (int)testValue;
    } else {
        NSLog(@"empty");
        viewController.myNum = 0;
    }

    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {

    NSNumber *num = [NSNumber numberWithInt:viewController.myNum];

    NSDictionary *savedNumber = [NSDictionary dictionaryWithObject:num forKey:@"kRestore"];
    [[NSUserDefaults standardUserDefaults] registerDefaults:savedNumber];
    [[NSUserDefaults standardUserDefaults] synchronize];

    NSLog(@"ok resign active");
}

Thanks

Paul
  • 6,108
  • 14
  • 72
  • 128

3 Answers3

1

Try setting the viewController.myNum = [testValue intValue]; instead of using (int):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    NSNumber *testValue = [[NSUserDefaults standardUserDefaults] objectForKey:@"kRestore"];
    if (testValue){
        NSLog(@"already");
        viewController.myNum = [testValue intValue];
    } else {
        NSLog(@"empty");
        viewController.myNum = 0;
    }
    return YES;
}

Then just use setObject:forKey: like so:

- (void)applicationWillResignActive:(UIApplication *)application {
    NSNumber *num = [NSNumber numberWithInt:viewController.myNum];
    [[NSUserDefaults standardUserDefaults] setObject:num forKey:@"kRestore"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    NSLog(@"ok resign active");
}
chown
  • 51,908
  • 16
  • 134
  • 170
1

Try changing your applicationWilLResignActive: method to:

NSNumber *num = [NSNumber numberWithInt:viewController.myNum];

[[NSUserDefaults standardUserDefaults] setObject:num forKey:@"kRestore"];
[[NSUserDefaults standardUserDefaults] synchronize];

Also, you can't cast an NSNumber instance to an int. In your application:didFinishLaunchWithOptions: method, change (int)testValue to [testValue intValue].

Ash Furrow
  • 12,391
  • 3
  • 57
  • 92
1

Calling -registerDefaults in applicationWillResignActive makes little sense. See my answer here on how registerDefaults works: How to register user defaults using NSUserDefaults without overwriting existing values?

You use it to set the default values. To save a value that is not the default but the users preference, use the various -set.. methods provided.

Community
  • 1
  • 1
Johan Kool
  • 15,637
  • 8
  • 64
  • 81
  • thanks, i don't understand how you use it : you use `registerDefaults` to save the plist file on disk (with register...), but when do you save the `number` into the plist file? And also, do you mean `Defaults.plist`is a default file already created by xcode, or could i name it `def.plist`, and just create a `NSNumber` row with the number in the dictionary (in the plist)? thanks, and sorry i'm not yet familiar with this kind of stuff ;) – Paul Sep 30 '11 at 18:45
  • No, `registerDefaults` is used to set the default defaults: what the app uses when the user hasn't changed his settings yet. The plist file in the linked answer is a file I created myself to make it easy to have all such settings in one place. You can name it anything you like, but there's little point in that. – Johan Kool Oct 01 '11 at 18:13