UINavigationController not showing the Root View controller
I have a UIView (menuView in code below) of size 320x218 inside a view. I want to load a navigation controller into this view. Im using the following code to do that:
MenuViewController *menuController = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:menuController];
navigationController.navigationBarHidden = YES;
[menuView addSubview:navigationController.view];
[menuController release];
[navigationController release];
When I execute it, the root view is not displayed in that view. Only a navigation bar is displayed and the rest of the view is empty.
Edit:
I just placed an NSLog() 开发者_C百科in both initWithNibName: and viewDidLoad: of MenuViewController. The one in initWithNibName: gets called but the one in viewDidLoad: doesn't :S
Update:
I tried to push menuController
to my navigationController
thinking since its not appearing, it might not be on the stack. Exception:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing the same view controller instance more than once is not supported
call layoutsubviews do work.
[super loadView];
[self.view addSubview:navigationController.view];
[navigationController.view layoutSubviews];
I found the answer here:
UIViewController -viewDidLoad not being called
I had to add these lines of code after -initWithRootViewController
in order to load the view of my root view Controller:
navigationController.navigationBarHidden = YES;
[navigationController setView:menuController.view];
You should not add the navigationViewController as an subview To your MenuViewController. As the navigationViewController already already holds the MenuViewController.
Just display the navigationViewController.
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
ViewController *viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
navController = [[UINavigationController alloc]initWithRootViewController:viewController];
self.window.rootViewController = self.navController;
Try this code in your appdelegate method
精彩评论