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

13,400

Solution 1

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.

Solution 2

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.

Solution 3

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.)

Solution 4

Felixyz answer did the trick for me. Calling the view will appear method will run the code in it every time the view appears. Different from view did load, which runs its code only when the view is first loaded. So your parent view would not update itself if a child view altered the info displayed in the parent, and was then popped off the sack. But if the parents calls view will appear, the code gets ran every time the view shows back up.

Make sure to call the super method at the same time. Proper implementation would look like this:

- (void)viewWillAppear:(BOOL)animated
{
   [super viewWillAppear:animated];
    NSLog(@"View Appearing");
}
Share:
13,400
TomAnth
Author by

TomAnth

Updated on June 04, 2022

Comments

  • TomAnth
    TomAnth almost 2 years

    I need to update the parent view on an iPhone after popping a child view off the navigation stack. How can I setup the parent view to be notified or receive an automatic method call when the child is popped off the stack and the parent becomes visible again?

    The user enters data on the child page that I want to display on the parent page after the user is done and pops the view.

    Thanks for you help!

  • Jean-Denis Muys
    Jean-Denis Muys almost 12 years
    which is cumbersome if your child controller has itself a child. In that case viewWillDisappear is called when the child pushes the grandchild as well as when the child is popped. Only in the latter case would you want to message the parent. I am yet to find an elegant way out of that issue.
  • Marcos Reboucas
    Marcos Reboucas almost 11 years
    ur observation was very important....unforgiven3 answer is correct but if u don't implement the UINavigationControllerDelegate protocol in the ViewController where ur calling willShowViewController it will never works, and also the parent ViewController will keep complaining ur sending "ViewController __strong to parameter of imcompatible type id <UINavigationControllerDelegate>