views:

986

answers:

3

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!

+3  A: 

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.

unforgiven3
+1  A: 

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
A: 

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.

MikZ