[英]navigationController is nil,when push the viewcontroller
There are three viewController
, MainViewController
ViewControllerB
and ViewControllerC
. 有三个viewController
, MainViewController
ViewControllerB
和ViewControllerC
。 MainViewController
will be loaded when the app launch. 应用启动时将加载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;
}
and there is a button
on the MainViewController
, present ViewControllerB
, 在MainViewController
上有一个button
,现在是ViewControllerB
,
UIViewController *rootViewController = [[UIApplication sharedApplication].keyWindow rootViewController];
ViewControllerB * vcb=[[ViewControllerB alloc] init];
[rootViewController presentViewController:vcb animated:YES completion:nil];
After the ViewControllerB
appear, click the button push ViewController
C. but the navigationController
is nil
. 在出现ViewControllerB
之后,单击按钮ViewController
。但是navigationController
为nil
。 It can't push ViewControllerC
它不能推ViewControllerC
[self.navigationController pushViewController:vcC animated:YES];
You have the MainViewController ( mvc ), which is embedded in a NavigationController ; 您有MainViewController( mvc ),它嵌入在NavigationController中 ;
Then, on mvc you have the following code: 然后,在mvc上,您具有以下代码:
[rootViewController presentViewController:vcb animated:YES completion:nil];
You are calling presentViewController
on the current ViewController, which will modally present the next ViewController, in this case ViewControllerB ( vcb ); 您正在当前的ViewController上调用presentViewController
,它将以模式方式显示下一个ViewController,在本例中为ViewControllerB( vcb );
Finally, you try to access the NavigationController inside ViewControllerB ( vcb ) in order to push ViewControllerC ( vcc ), with the following code: 最后,您尝试使用以下代码访问ViewControllerB( vcb )内的NavigationController以推送ViewControllerC( vcc ):
[self.navigationController pushViewController:vcC animated:YES];
The problem is that vcb is not aware of the NavigationController , since presentViewController
presents the view controller modally, outside the existing navigation stack. 问题在于vcb无法presentViewController
NavigationController ,因为presentViewController
在现有导航堆栈之外以模态presentViewController
呈现视图控制器。 Thus, resulting in a nil NavigationController in vcb . 因此,在vcb中产生nil NavigationController 。
You can refer to https://stackoverflow.com/a/14233252/9323816 for more information. 您可以参考https://stackoverflow.com/a/14233252/9323816了解更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.