【发布时间】:2012-02-10 17:16:16
【问题描述】:
我需要确定 UIViewController 之前在 UINavigationController 中显示的内容。我的导航深度为 3 级,在第 2 级中,我需要确定我是从第 1 级推送到这里还是从第 3 级弹出到这里。我怎样才能轻松做到这一点?
【问题讨论】:
标签: objective-c ios cocoa-touch uinavigationcontroller
我需要确定 UIViewController 之前在 UINavigationController 中显示的内容。我的导航深度为 3 级,在第 2 级中,我需要确定我是从第 1 级推送到这里还是从第 3 级弹出到这里。我怎样才能轻松做到这一点?
【问题讨论】:
标签: objective-c ios cocoa-touch uinavigationcontroller
实现 UINavigationControllerDelegate 方法:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
并查询它以找出当前显示的视图控制器:
navigationController.topViewController
这就是你的来历。
【讨论】:
UINavigationControllerDelegate 方法。当我NSLog(@"%@",navigationController.topViewController); 时,我只得到当前正在显示的UIViewController(2 级)(不是我导航的 3 级视图控制器或 1 级视图控制器)。
您可以使用 viewControllers 属性查看整个 UINavigationController 堆栈。
int count = [navigationController.viewControllers count];
topController = [navigationController.viewControllers objectAtIndex:count - 1];
previousController = [navigationController.viewControllers objectAtIndex:count - 2];
//...
//...
rootController = [navigationController.viewControllers objectAtIndex: count - count];
【讨论】:
iOS 5.0 添加了[UIViewController isMovingToParentViewController]。在viewWillAppear 和viewDidAppear 期间,如果您来自编号较小的视图控制器,则返回YES,否则返回NO。不幸的是,这个名字令人困惑——你会认为[UIViewController isMovingFromParentViewController] 是正确的调用方法。
但是,我代表所有第二代设备卡在 iOS 4.2.1 的小气鬼,请不要使用 iOS 5 的功能,除非你真的必须这样做。
【讨论】: