How to automatically call a method after popping a view controller off the stack on the iPhone

I just resolved this self same problem - and the answers above are almost correct, they just forgot about setting the delegate.

I have a root view controller that displays the size of a list, calls a child view controller that may alter the size of a list, and must update the size upon return.

When I create my parent view (SettingsView below), and add it as the root view of a UINavigationController, I make sure to set the UINavigationController's delegate before I display the view - that's the key part:

SettingsView *sv = [[SettingsView alloc] initWithNibName:@"SettingsView" bundle:nil];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:sv];
[nc setDelegate:sv];

In the parent view, implement the UINavigationControllerDelegate protocol:

@interface SettingsView : UIViewController <UINavigationControllerDelegate>

and provide the willShowViewController method:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    // Your code to update the parent view
}

This is called after the child view is dismissed, and before the parent view is redisplayed.


I had the need to do something like this as well. In the ViewController that owned my UINavigationController, I had to implement willShowViewController, like this:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
}

That method is called whenever the UINavigationController changes views. If I'm understanding your question correctly, I think this should do what you want.


I think there is some confusion here. UIViews are not pushed to and popped from the UINavigationController's stack. What is being pushed and popped is UIViewControllers, which in turn handle one or (more often) several views each.

Fortunately, the UIViewController has these methods:

-(void) viewWillAppear:(BOOL)animated;
-(void) viewDidAppear:(BOOL)animated;
-(void) viewWillDisappear:(BOOL)animated;
-(void) viewDidDisappear:(BOOL)animated;

These are called whenever the view is about to (dis)appear, or has just (dis)appeared. I works with tab bars, modal views and navigation controllers. (And it's a good idea to make use of these when you implement custom controllers.)

So in your case, if I understand correctly, you simply have to override the viewWillAppear: or viewDidAppear: method on what you call the "parent page" (which is presumably handled by a UIViewController) and put in code to update the appearance of the page to reflect the data just entered.

(If I remember correctly, you must make sure that the UINavigationController gets a viewWill/DidAppear: message when it is first displayed, in order for these messages to later be sent to its child controllers. If you set this up with a template or in IB you probably don't have to worry about it.)