0

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?

cannyboy
  • 24,180
  • 40
  • 146
  • 252

4 Answers4

1

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
  • 109,922
  • 25
  • 130
  • 137
  • 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 Jul 06 '10 at 12:00
  • Thank you for the clarification. I removed my comment. – falconcreek Jul 06 '10 at 12:20
1

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
  • 4,170
  • 1
  • 21
  • 22
0

You can determine this directly via a couple of methods on your UIViewController subclass.

From Apple's documentation:

Occasionally, it can be useful to know why a view is appearing or disappearing. For example, you might want to know whether a view appeared because it was just added to a container or whether it appeared because some other content that obscured it was removed. This particular example often appears when using navigation controllers; your content controller’s view may appear because the view controller was just pushed onto the navigation stack or it might appear because controllers previously above it were popped from the stack.

The UIViewController class provides methods your view controller can call to determine why the appearance change occurred.

  • isMovingFromParentViewController: view was hidden because view controller was removed from container
  • isMovingToParentViewController: view is shown because it's being added to a container
  • isBeingPresented: view is being shown because it was presented by another view controller
  • isBeingDismissed: view is being hidden because it was just dimissed
wallace
  • 454
  • 5
  • 18
0

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
  • 33,180
  • 5
  • 60
  • 80