导航控制器为零,当推送视图控制器时 [英] navigationController is nil,when push the viewcontroller

查看:35
本文介绍了导航控制器为零,当推送视图控制器时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

共有三个viewControllerMainViewControllerViewControllerBViewControllerC.MainViewController 将在应用启动时加载.

There are three viewController, MainViewController ViewControllerB and ViewControllerC. MainViewController will be loaded when the app launch.

- (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

and there is a button on the MainViewController, present ViewControllerB,

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

ViewControllerB出现后,点击按钮pushViewControllerC.但是 navigationControllernil.它不能推送 ViewControllerC

After the ViewControllerB appear, click the button push ViewControllerC. but the navigationController is nil. It can't push ViewControllerC

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

推荐答案

  1. 您有 MainViewController (mvc),它嵌入在 NavigationController 中;

然后,在 mvc 上,您有以下代码:

Then, on mvc you have the following code:

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

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

You are calling presentViewController on the current ViewController, which will modally present the next ViewController, in this case ViewControllerB (vcb);

最后,您尝试访问 ViewControllerB (vcb) 内的 NavigationController 以推送 ViewControllerC (vcc),使用以下代码:

Finally, you try to access the NavigationController inside ViewControllerB (vcb) in order to push ViewControllerC (vcc), with the following code:

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

问题在于 vcb 不知道 NavigationController,因为 presentViewController 在现有导航堆栈之外以模态呈现视图控制器.因此,导致 vcb 中的 nil NavigationController.

The problem is that vcb is not aware of the NavigationController, since presentViewController presents the view controller modally, outside the existing navigation stack. Thus, resulting in a nil NavigationController in vcb.

您可以参考https://stackoverflow.com/a/14233252/9323816 了解更多信息.

You can refer to https://stackoverflow.com/a/14233252/9323816 for more information.

这篇关于导航控制器为零,当推送视图控制器时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆