我在导航 View 层次结构的上下文中切换 View ,我希望能够在切换时确定在新 View 下推送的先前 View 是什么。
我正在 UINavigationControllerDelegate 中尝试:
(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
NSLog( @"Switching from %@ to %@",
NSStringFromClass( [[navigationController visibleViewController] class] ),
NSStringFromClass( [viewController class] )
);
}
我明白了:
2009-08-05 20:05:21.274 App Name [85913:20b] Switching from ManagementScreen to ManagementScreen
不幸的是,似乎在调用“will”之前,它已经在 UINavigationController 的状态中被换出,这样传入的 viewController 始终与 UINavigationController 上的 visibleViewController 相同(还有 topViewController 属性,此处未演示)但我用相同的代码试过了)。
我想避免扩展导航 View Controller ,老实说,虽然我可以轻松地在委托(delegate)上放置一个属性 - 但是我想知道这种行为在现有框架中是否可行(似乎应该在它发生之前调用之后发生了什么,但导航 Controller 的状态似乎在这两者之前都被修改了)。
谢谢!
最佳答案
我认为使用 UINavigationControllerDelegate 的答案不起作用,因为正如问题所指出的那样,在调用委托(delegate)时,将要显示的 View Controller 已经是 navigationController.topViewController 和 navigationController.visibleViewController 的值。
相反,使用观察者。
第 1 步。设置观察者以观察 UINavigationControllerWillShowViewController 通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewControllerChange:) name:@"UINavigationControllerWillShowViewControllerNotification" object:self.navigationController];
第 2 步。创建通知回调(在此示例中称为 viewControllerChange)并使用通知 userInfo 字典中的键来查看上一个和下一个 View Controller :
(void)viewControllerChange:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
NSLog(@"Switching from %@ to %@", [[userInfo objectForKey:@"UINavigationControllerLastVisibleViewController"] class], [[userInfo objectForKey:@"UINavigationControllerNextVisibleViewController"] class]);
}
关于iphone - 如何判断UINavigationControllers中的prior visibleViewController是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1236380/