views:

602

answers:

1

I have a UIViewController that manages the display of some data. When the user wants to edit this data I push an edit UIViewController onto the stack. When the user is finished editing the top view controller is popped off the stack. What is the most elegant way to know that I need to update my display after the edit view is popped off?

I thought that I might be able to put the content update code into the viewDidLoad method of my data view, but this method isn't always called when my view displays, especially not when I'm navigating down the view stack.

I also considered setting up my data view controller as a delegate for the UINavigationController and wait for – navigationController:didShowViewController:animated: to be called, my concern is that there may be other view controllers that need to be notified when they're displayed and it will turn into a minor headache managing which controller should be receiving the didShowViewController message.

+4  A: 

I think viewWillAppear will do the trick.

Otherwise your edit view controller could call a new method of delegate pointing to your parent controller. In there, you can update the data model and display. For instance:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if ([delegate respondsToSelector:@selector(editEntryByTitle:)])
  [delegate performSelector:@selector(editEntryByTitle:) withObject: textField.text];
    [textField resignFirstResponder];
    [self dismissModalViewControllerAnimated:YES];
    return YES;
}
leonho
viewWillAppear worked like a charm, thanks!
kubi