UIViewController没有弹出到root?

时间:2016-01-15 00:00:50

标签: objective-c uiviewcontroller uinavigationcontroller uistoryboard uistoryboardsegue

一些细节:

InitialController是故事板入口点。在那里,我检查我是否登录并执行segue到主应用程序页面,或者如果我不是,那么我执行segue登录控制器。

即。在InitialController(我是UINavigationController)我有

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidLoad];
    if ([already logged in]) {
        [self performSegueWithIdentifier:@"toMain" sender:self];
    } else {
        [self performSegueWithIdentifier:@"toLogin" sender:self];
    }
}

如果我进入登录路线,我会到达我的视图控制器看起来像这样的点(使用内部_printHierarchy方法)

<InitialController 0x7fbdfc81c200>, state: appeared, view: <UILayoutContainerView 0x7fbdfa3b8910>
   | <LoginController 0x7fbdfa01be50>, state: disappeared, view: <UIView 0x7fbdf9631670> not in the window
   | <AnotherController 0x7fbdfe286e00>, state: appeared, view: <UIView 0x7fbdfe12b680>

当我在AnotherController上完成操作时,我调用popToRootViewController ...这令我烦恼地转到LoginController,但我试图通过将此方法添加到LoginController来解决此问题

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (...) { // is only true when I want it to be true
        [self.navigationController popToRootViewControllerAnimated:NO];
    }
}

我设置了一个断点,在我调用这个popToRoot方法之前再次打印层次结构......我得到了:

<InitialController 0x7fbdfc81c200>, state: appeared, view: <UILayoutContainerView 0x7fbdfa3b8910>
   | <LoginController 0x7fbdfa01be50>, state: appeared, view: <UIView 0x7fbdf9631670>

但它仍然留在LoginController,尽管我想回到InitialController。为什么不让我回到InitialController? 我猜这可能与他们都出现的事实有关,所以也许LoginController被某种方式视为根或与UILayoutContainerView有关?

旁注:我还尝试不将InitialController设为UINavigationController,然后视图层次结构变为:

<InitialController 0x7fcddac9da30>, state: disappeared, view: <UIView 0x7fcddb35e500> not in the window
   + <LoginController 0x7fcddaf02eb0>, state: appeared, view: <UIView 0x7fcddb3635a0>, presented with: <_UIFullscreenPresentationController 0x7fcddb103700>

再次不允许我弹出InitialController

1 个答案:

答案 0 :(得分:0)

UINavigationController是一个管理一堆viewControllers(不包含自身)的容器。 UINavigationController的rootViewController是该堆栈中的第一个viewController,因此popToRootViewControllerAnimated将引导您进入。 您不应该将您的内容直接放入UINavigationController。

我的建议是让storyboard入口点成为一个单独的UINavigationController,然后包含InitialController作为其堆栈中的第一个viewController(应该是UIViewController,而不是UINavigationController)。然后当你在UINavigationController上调用popToRootViewControllerAnimated时,你将被带到InitialController。