iphone - UITabBarController 中的 UINavigationController 存在 View 生命周期问题

标签 iphone ios

我的 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/

相关文章:

ios - Http 失败响应 501 : Not implemented, 完全相同的方法适用于网络

ios - 将字符串转换为 int 以检查答案

ios - 切换 View Controller 后第二次调用方法的问题

ios - UITableView不显示通讯簿

ios - 如何将推送通知发送到 iOS 设备上的 Web 应用程序?

iphone - 将通用应用程序作为缩放的 iPhone 应用程序运行?

ios - xcode 6 升级导致 prepare for segue lag showing screen

iphone - 使用 SecKeyWrapper 中断加密 16 个字节的 UTF8 (ccStatus == -4304)

iphone - 如何为 NSPredicate 连接两个字符串,即名字和姓氏

iOS 应用程序在 ogg vorbis 库上随机崩溃