views:

473

answers:

4

Is there a way for a UIViewController (inside a navigation stack) to detect whether it is appearing because a drill-down or a drill-up was performed?

In viewWillAppear, the UINavigationController's topViewController and visibleViewController are already set to the new ViewController, unfortunately.

A: 

Couldn't you look at the viewControllers property in the navigation controller?

drvdijk
I'm afraid, not. The current view controller will always be the last item in the array. Whether this happend as a result of itself beeing pushed or another controller beeing popped cannot be detected. :(
Norman
+2  A: 

You could subclass UINavigationController, and add a property didPushViewController. Then you can override pushViewController and popViewController to correctly set the property to true or false respectively.

Matt Bridges
Sounds good. I just thought there was an easier way I overlooked. Thanks.
Norman
+1  A: 

Do you simply need to know what the previous and next view controllers will be? Or do you need to specifically know whether a view controller was popped or pushed? You can implement the following method, which is defined by UINavigationControllerDelegate:

- ( void )navigationController:( UINavigationController * )navigationController willShowViewController:( UIViewController * )viewController animated:( BOOL )animated
{
    UIViewController * currentController = navigationController.visibleViewController;
    UIViewController * nextController = viewController;

    // Do whatever here.
}

If, however, you DO need to know whether a particular view controller was popped or pushed, then Matt Bridges' suggestion is the way to go.

LucasTizma
+1  A: 

Another way is to stash any view controllers you are drilling down to as class local variables - then in viewWillAppear, you know you were hit because of a drill-up if any of the class local variables are still set. You even know which controller the user returned from, so you can do different logic (like fetching changed values from the view controllers you drilled down to).

Don't forget to release and nil out the references in viewWillAppear so that the system is reset to recognize things properly again.

I like this mechanism more than having the drill-down controllers know about the master view as a delegate to push changes out, as often they are working on some separate small piece of data and shouldn't have to know about a whole master view controller. It makes them easier to reuse as well since they can be called up by many different classes.

Kendall Helmstetter Gelner