我认为使用uinavigationControllerDelegate的答案不起作用,因为正如问题所指出的那样,到调用该委托时,将显示的视图控制器已经是navigationController.topViewController和navigationController.visibleViewController的值。
相反,使用观察者。
步骤1。设置观察者以监视uinavigationcontrollerwillshowviewcontroller通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewControllerChange:) name:@"UINavigationControllerWillShowViewControllerNotification" object:self.navigationController];
步骤2。创建通知回调(在本例中称为viewcontrollerchange),并使用通知用户信息字典中的键查看最后一个和下一个视图控制器:
(void)viewControllerChange:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
NSLog(@"Switching from %@ to %@", [[userInfo objectForKey:@"UINavigationControllerLastVisibleViewController"] class], [[userInfo objectForKey:@"UINavigationControllerNextVisibleViewController"] class]);
}