问题
This is what I am doing. I have a tabBarControllerOne
with 5 tabs. On clicking one of the tabs, I present a modal view controller, which has a navigationBar and a TabBarControllerTwo
(with 3 tabs). These three tabs are the matter for concern here.
In the 5th Tab of tabBarController
I show modalViewController as
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.nextTabView];
// navController.navigationBarHidden = YES;
navController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
NSLog(@"Displauing the navcontroller before pushing %@", navController);
[self presentModalViewController:navController animated:NO];
Here, nextTabView
is a tabBarController with 3 tabs. The views work. In the views, if I try something like.
self.navigationController.navigationBarHidden = YES;
[self.navigationController pushViewController: someController animated:YES];
// nothing works.
If I NSLog, it displays self.navigationController
as (null)
Can someone tell me why this is not working ?
回答1:
Embedding a UITabBarController
inside a UINavigationController
is not supported. Apple has a careful hierarchy of container view controllers, and a UITabBarController
must be the root of its view controller hierarchy.
Additionally, as Joe points out, your views don't belong to the navigation controller; they belong to the tab bar controller, so their navigationController
property is not set.
回答2:
The modal view controller does not belong to a UINavigationController
stack therefore the property is not set. You will want to use delegation to notify the creating controller when something is selected then that controller can properly push the next controller on to the stack.
UIViewController Reference:
Discussion
Only returns a navigation controller if the view controller is in its stack. This property is nil if a navigation controller cannot be found.
来源:https://stackoverflow.com/questions/6902380/why-is-self-navigationcontroller-null-when-pushed-from-uitabbarcontroller-subvie