0

Lets say I have two viewcontrollers A and B. From A to B, I add the viewcontroller B on top of A. On popping, I call the following method in B

 -(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    NSUserDefaults *userSettings = [NSUserDefaults standardUserDefaults];
    [userSettings setObject:firstName.text forKey:@"FN"];
    [userSettings setObject:lastName.text forKey:@"LN"];
    [userSettings synchronize];
 }

This userInfoUpdate method updates the NSUserDefaults object for the application. On returning back to viewcontroller A, I call the following method in A:

- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
  NSUserDefaults *userSettings = [NSUserDefaults standardUserDefaults];
  profileTextField.text = [NSString stringWithFormat:@"%@ %@",[userSettings objectForKey:@"FN"],[userSettings objectForKey:@"LN"]];
}

But this doesn't update textfiled string. Any idea what I'm doing wrong here? Is there a better alternative?

Siddharthan Asokan
  • 4,321
  • 11
  • 44
  • 80

2 Answers2

1

I would recommend creating a class that acts as your data model that can be shared across the 2 Controllers. That way you would not have to worry about the Viewcontroller delegate function order.

@interface DataModel: NSObject
{
    NSString *firstName;
    NSString *lastName;
}

A contains object for DataModel:

@interface AVC:UIViewController
    @property (nonatomic, strong) DataModel *myDataModel

When A creates B and pass myDataModel to be updated by B and save it in viewDidDissapear.

A will now have updated value when it comes back in view and also saved across app launches and upgrades

hackerinheels
  • 1,141
  • 1
  • 6
  • 14
0

You don't call [userSettings synchronize] to save them.

antonio
  • 375
  • 4
  • 13
  • What happens if you update your field in viewDidAppear instead? – antonio Jul 10 '14 at 21:25
  • Also don't forget to call [super viewWillAppear:animated] and [super viewWillDisappear:animated] in corr.methods. Otherwise your app will behave incorrectly. – antonio Jul 10 '14 at 21:27