开发者

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 ou开发者_Go百科t which of these happened?


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.


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.


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜