navigationController 为 nil,当按下 viewcontroller

navigationController is nil,when push the viewcontroller

viewControllerMainViewControllerViewControllerBViewControllerC三个。 MainViewController 将在应用启动时加载。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];

    MainViewController * main = [[MainViewController alloc]init];
    UINavigationController * navigationController = [[UINavigationController alloc]initWithRootViewController:main];
    self.window.rootViewController = navigationController;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    return YES;       
}

并且MainViewController上有一个button,现在ViewControllerB

UIViewController *rootViewController = [[UIApplication sharedApplication].keyWindow rootViewController];
ViewControllerB * vcb=[[ViewControllerB alloc] init];
[rootViewController presentViewController:vcb animated:YES completion:nil];

出现ViewControllerB后,点击按钮push ViewControllerC。 但是 navigationControllernil。它无法推送 ViewControllerC

[self.navigationController pushViewController:vcC animated:YES];
  1. 您有 MainViewController (mvc),它嵌入在 NavigationController 中;

  2. 然后,在 mvc 上你有以下代码:

    [rootViewController presentViewController:vcb animated:YES completion:nil];

    您在当前 ViewController 上调用 presentViewController,它将 模态呈现 下一个 ViewController,在本例中 ViewControllerB (vcb);

  3. 最后,你尝试依次访问ViewControllerB(vcb)里面的NavigationController使用以下代码推送 ViewControllerC (vcc):

    [self.navigationController pushViewController:vcC animated:YES];

问题是 vcb 不知道 NavigationController,因为 presentViewController 在外部以模态方式呈现视图控制器现有的导航堆栈。因此,导致 nil NavigationControllervcb.

您可以参考了解更多信息。