2
votes

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() 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
4
Does MenuViewController.xib exist and the name is correct? - Praveen S
Yes and yes. I just placed an NSLog() in both initWithNibName: and viewDidLoad: of MenuViewController. The one in initWithNibName: gets called but the one in viewDidLoad: doesn't :S - Bushra Shahid
Ok after reading your comment, can you place a line menuController.view; after you create the navigationController? Just add that line and let me know the result. - Praveen S
just like menuController.view; or put it in NSLog()??? - Bushra Shahid
Just add that line as part of code, you will get a warning but thats ok. - Praveen S

4 Answers

8
votes

call layoutsubviews do work.

[super loadView];
[self.view addSubview:navigationController.view];
[navigationController.view layoutSubviews];
3
votes

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];
0
votes

You should not add the navigationViewController as an subview To your MenuViewController. As the navigationViewController already already holds the MenuViewController.

Just display the navigationViewController.

0
votes
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