Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Identify what UIViewController was previously shown in a UINavigationController

I need to identify what UIViewController was previously shown in a UINavigationController. My navigation is 3 levels deep, in level 2 I need to identify if I got here from pushing from level 1 or if I got here from popping from level 3. How can I do this easily?

like image 348
Peter Warbo Avatar asked Feb 10 '12 17:02

Peter Warbo


2 Answers

Implement the UINavigationControllerDelegate method:

 - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated

and query it to find out the currently displayed view controller:

 navigationController.topViewController

This is the one you're coming from.

like image 104
Rayfleck Avatar answered Nov 03 '22 07:11

Rayfleck


You can look at the entire UINavigationController stack by using the the viewControllers property.

int count = [navigationController.viewControllers count];

topController = [navigationController.viewControllers objectAtIndex:count - 1];
previousController = [navigationController.viewControllers objectAtIndex:count - 2];
//...
//...
rootController = [navigationController.viewControllers objectAtIndex: count - count];
like image 40
Joel Kravets Avatar answered Nov 03 '22 06:11

Joel Kravets