views:

44

answers:

4

Using a UINavigationViewController, how do I find out how a view has appeared?

The view has either appeared in a straightforward manner, as the first view in the UINavigationController stack. Or it has appeared because a second view has been popped and the first view has revealed itself again. How do you find out which of these happened?

A: 

The only reliable way to do this, as far as I'm aware, is to subclass UINavigationController and override the UINavigationBarDelegate methods:

– navigationBar:shouldPushItem:
– navigationBar:didPushItem:
– navigationBar:shouldPopItem:
– navigationBar:didPopItem:

Don't forget to call super, of course.

Can Berk Güder
From the `UINavigationBar` documentation regarding the `delegate` property: "If the navigation bar was created by a navigation controller and is being managed by that object, you must not change the value of this property. Navigation controllers act as the delegate for any navigation bars they create."
Can Berk Güder
Thank you for the clarification. I removed my comment.
falconcreek
A: 

Depending on your reasons for knowing the difference, you can also manipulate variables in the viewWillAppear/viewWillDisappear methods of the affected views.

Take a look at this SO question.

iPhoneDollaraire
+1  A: 

Simple approach is to add a property to your RootViewController to track whether or not it has pushed another view onto the navigationController.

-(BOOL)hasPushedSecondView;

Initialize to NO in your init method.

Before pushing secondViewControllers view onto the stack, update the property to YES.

In viewWillAppear, check the value and update your view accordingly. Depending on how you want the application to behave you may need to reset the hasPushedsecondview property back to NO.

falconcreek
A: 

you could take a look at the leftBarButtonItem or backBarButtonItem, based on how your application is written and determine how the view appeared. If it is on top, unless you have a custom leftBarButtonItem, there would be no object there.

Aaron Saunders