cannyboy
cannyboy

Reputation: 24426

How to tell if view has appeared via popping or not?

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?

Upvotes: 0

Views: 1005

Answers (4)

wallace
wallace

Reputation: 454

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

Upvotes: 0

Aaron Saunders
Aaron Saunders

Reputation: 33345

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.

Upvotes: 0

falconcreek
falconcreek

Reputation: 4170

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.

Upvotes: 1

Can Berk Güder
Can Berk Güder

Reputation: 113310

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.

Upvotes: 1

Related Questions