2

I have a UIView animation in viewWillAppear: method, behavior of the animation is related to ControlView's movement. If NavigationController is pushing the View the animation should play in normal, if View is pulled back the animation should play in reverse. How can I find out if the current ViewController is being pushed or pulled in viewWillAppear: method?

Hadi Sharghi
  • 903
  • 16
  • 33

2 Answers2

3

In iOS7 you can implement UINavigationController delegate method

- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC;

and get info from UINavigationControllerOperation operation

typedef NS_ENUM(NSInteger, UINavigationControllerOperation) {
    UINavigationControllerOperationNone,
    UINavigationControllerOperationPush,
    UINavigationControllerOperationPop,
};

Documentation says:

Called to allow the delegate to return an interactive animator object for use during view controller transitions.

Implement this delegate method when you want to provide a custom, interactive transition between view controllers as they are added to or removed from the navigation stack. The object you return should configure the interactivity aspects of the transition and should work with the object in the animationController parameter to start the animations.

The animator object responsible for managing the transition animations, or nil if you want to use the standard navigation controller transitions.

Thus, in the case of simple observing of transition you should return nil in this method.

malex
  • 9,874
  • 3
  • 56
  • 77
  • Thanks, could you give me more explanation how to use it? I added `UIViewControllerAnimatedTransitioning` delegate to my `viewController` .h file. but this method is not getting called. And I don't know what should be returned? `self`? – Hadi Sharghi Jun 01 '14 at 09:08
  • @Hadu you need set you view controller to be the delegate for its navigation controller. In loadView method you need to add self.navigationController.delegate=self. Then the method described in my answer will be called each time at pop and push. – malex Jun 01 '14 at 12:04
  • Could you please tell me what should be returned in the method? – Hadi Sharghi Jun 06 '14 at 06:03
  • 1
    @Hadu please see explanation in the text extended. – malex Jun 06 '14 at 06:41
  • Note that implementing this delegate method disables the navigation controller's interactive pop gesture recognizer. See [this answer](http://stackoverflow.com/a/38859457/5352503) for how to re-enable it. – jamesk Aug 09 '16 at 20:06
0

You can use the UINavigationController's viewControllers property:

@property(nonatomic, copy) NSArray *viewControllers

According to Apple Documentation: The root view controller is at index 0 in the array, the back view controller is at index n-2, and the top controller is at index n-1, where n is the number of items in the array.

You can print the index of viewControllers Array and it will let you know that on which viewController you are standing now.

NSArray * viewControllers = [[self navigationController] viewControllers];
NSLog(@"%@",viewControllers.description);
Irfan
  • 4,301
  • 6
  • 29
  • 46
  • I don't want to know which viewController I'm standing on, I want to know from which viewController I'm coming from. – Hadi Sharghi Jun 01 '14 at 00:13
  • simple when you print the viewControllers array it will print the viewcontrollers name from root to onwards.And hence you will know that from which viewController you are coming from. – Irfan Jun 01 '14 at 00:16
  • No matter if I'm going to or coming bak from another view controller, always print same number of viewControllers. (In my case 2 viewControllers) – Hadi Sharghi Jun 01 '14 at 04:25