我的 NavigationController 中遇到一些有问题的行为。我在 TabBarController 中设置了一些 UINavigationController(具体是 4 个选项卡)。我的 AppDelegate 符合 UITabBarControllerDelegate,我用它在切换选项卡时触发 popToRootViewController,因此当用户返回该选项卡时,它会返回到 rootViewController。一切正常,除了:“返回”到该选项卡时,在加载 rootView 的生命周期之前调用最后加载的 viewController 的 viewWillAppear 。 我的委托(delegate)实现如下所示;
#pragma -mark TabBarController
///////////////Pop our navigationControllers to the rootView when Tab is changed////////////////////////////////
- (void) tabBarController: (UITabBarController *) tabBarController didSelectViewController: (UIViewController *) viewController {
if ([viewController isKindOfClass:[UINavigationController class]]) {
NSLog(@"******POP TO ROOT VIEW*******");
[(UINavigationController*)viewController popToRootViewControllerAnimated:NO];
}
}
现在我意识到委托(delegate)方法是didSelectViewController,但是有什么方法可以防止这种行为。类似于 did*Deselect*ViewController 的东西会很好,但 API 没有提供。我真的不能调用错误的 viewWillAppear 因为我在那里启动了一个多线程进程。有什么建议吗?
最佳答案
您可以通过实现 shouldSelectViewController: 并回答 YES 来实现相当于取消选择的效果:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
UIViewController *currentVC = tabBarController.selectedViewController;
// whatever you would like to do on deselect, like
[currentVC popToRootViewControllerAnimated:NO];
// it will be at the root when you get back to it, and as a bonus, you have
// a handle to viewController, which is about to be selected
return YES;
}
关于iphone - UITabBarController 中的 UINavigationController 存在 View 生命周期问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9857426/