How to determine which of the previous visible controls in UINavigationControllers?

I am switching views in the context of the navigation view hierarchy, and I want to be able to determine during the switchover that the previous view was pushed under a new view.

I am trying to do this in a UINavigationControllerDelegate:

(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { NSLog( @"Switching from %@ to %@", NSStringFromClass( [[navigationController visibleViewController] class] ), NSStringFromClass( [viewController class] ) ); } 

I get this:

2009-08-05 20: 05: 21.274 Application Name [85913: 20b] Switching from ManagementScreen to ManagementScreen

Unfortunately, it looks like it was already unloaded in the UINavigationController state before the “will” call, so the passed viewController always matches the visible visualization control in the UINavigationController (as well as the topViewController attribute, which is not shown here but I tried it with the same code )

I would like to avoid expanding the navigation view controller and, frankly, while I can easily place the property on the deletion, but I wonder if this behavior is possible within the existing structure (it seems like it needs to be called before this happens where as it was after, but it looks like the state of the navigation controller is modified before either).

Thanks!

+4
source share
2 answers
 - (void)navigationController:(UINavigationController*)nc didShowViewController:(UIViewController*)vc animated:(BOOL)animated { NSLog(@"Switching from %@ to %@", NSStringFromClass([vc class]), NSStringFromClass([[nc.viewControllers objectAtIndex:[nc.viewControllers count]-1] class])); } 
+2
source

I don't think the answers that use the UINavigationControllerDelegate work because, as the question indicates, by the time the delegate is called the view manager to be displayed, it is already a value for navigationController.topViewController and navigationController.visibleViewController.

Use observers instead.

Step 1. Configure an observer to observe the UINavigationControllerWillShowViewController notification:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewControllerChange:) name:@"UINavigationControllerWillShowViewControllerNotification" object:self.navigationController]; 

Step 2. Create a notification callback (in this example named viewControllerChange) and use the keys in the userInfo dictionary of notifications to see the last and next view controllers:

 (void)viewControllerChange:(NSNotification *)notification { NSDictionary *userInfo = [notification userInfo]; NSLog(@"Switching from %@ to %@", [[userInfo objectForKey:@"UINavigationControllerLastVisibleViewController"] class], [[userInfo objectForKey:@"UINavigationControllerNextVisibleViewController"] class]); } 
+5
source

All Articles