Reputation: 303
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;
}
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];
After the ViewControllerB
appear, click the button push ViewController
C.
but the navigationController
is nil
. It can't push ViewControllerC
[self.navigationController pushViewController:vcC animated:YES];
Upvotes: 4
Views: 12274
Reputation: 890
You have the MainViewController (mvc), which is embedded in a NavigationController;
Then, on mvc you have the following code:
[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);
Finally, you try to access the NavigationController inside ViewControllerB (vcb) in order to push ViewControllerC (vcc), with the following code:
[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. Thus, resulting in a nil NavigationController in vcb.
You can refer to https://stackoverflow.com/a/14233252/9323816 for more information.
Upvotes: 8