Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 418
joelhull Avatar asked Jun 16 '09 13:06

joelhull


3 Answers

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.

like image 81
cuhnayjun Avatar answered Oct 15 '22 00:10

cuhnayjun


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.

like image 36
Rob Avatar answered Oct 15 '22 01:10

Rob


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

like image 21
Felixyz Avatar answered Oct 15 '22 02:10

Felixyz