2

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
Bushra Shahid
  • 3,579
  • 1
  • 27
  • 37

4 Answers4

8

call layoutsubviews do work.

[super loadView];
[self.view addSubview:navigationController.view];
[navigationController.view layoutSubviews];
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
Aaron
  • 81
  • 1
  • 1
3

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];
Community
  • 1
  • 1
Bushra Shahid
  • 3,579
  • 1
  • 27
  • 37
0
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

Jig Patel
  • 82
  • 9
0

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

Just display the navigationViewController.

rckoenes
  • 69,092
  • 8
  • 134
  • 166