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

StackOverflow https://stackoverflow.com/questions/1001441

  •  05-07-2019
  •  | 
  •  

Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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

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");
}

If you need to notify one controller to another you may use delegation pattern as described here (see 2nd answer).

Unfortunately there is no automatic notification(AFAIK) for exact task as you described. To meet your needs you may send message to delegate (i.e. to your parent controller) in viewWillDisappear function of your child controller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top