views:

265

answers:

2

I have two view controller classes in my application delegate. I can change from one view controller to the next view controller by issuing the method -pushViewController:animated of the navigationController class and going back to the previous view controller by calling the method -popViewController:animated. Now my problem is I want to save some value to the preferences by using NSUserDefaults when I go back to the first view controller, and I don't know where should I put:

[[NSUserDefaults standardUserDefaults] setObject:@"value" forKey:@"key"];
+1  A: 

In your second view controller, add:

- (void) viewWillDisappear:(BOOL)animated {
  [[NSUserDefaults standardUserDefaults] setObject:@"value" forKey:@"key"];
  [super viewWillDisappear:animated];
}

As the second view disappears and the first view appears, the standard defaults will be updated.

Alex Reynolds
+1  A: 

These are all valid depending on exactly when you want to save the info:

-(void) viewWillAppear:(BOOL)animated;

-(void) viewDidAppear:(BOOL)animated;

-(void) viewWillDisappear:(BOOL)animated;
Andrew Johnson