I’m confused with this delegate message. I have a navigation controller in a PopupViewController. When I push a new viewController I wan’t to know what is the viewController that its being currently displayed, before the new one gets pushed…
I’ve tried getting navigationController.topViewController & navigationController.visibleViewContrller, but both of them always equal to the viewController that its going to be pushed… It looks like I’m receiving the message after the view was pushed
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
UIViewController *currentViewController = navigationController.topViewController;
if(currentViewController == viewController){
/// THIS IF STATEMENT IS ALWAYS TRUE
}
}
Has anyone stumbled upon anything similar?
Did some digging in the UINavigationController Class Reference and it looks like you can use the
viewControllersproperty of theUINavigationControllerclass.The root view controller (bottom of the navigation stack) is at index
0with the with the back view controller (the one that just got covered) at indexn-2and thetopViewControllerproperty being at indexn-1wherenis the number of view controllers in the array.As of iOS 7 and Xcode 5.x, you can now send the
firstObjectmessage to the array returned by theviewControllersproperty to get the root viewcontroller. Similarly, thetopViewControllerproperty can be accessed by sending thelastObjectmessage to the array returned by theviewControllersproperty.