sungl
sungl

Reputation: 1165

Help how to load UINavigationViewController from UIViewController

Here goes stupid question again :( I am trying to create a sample app that loads UIViewController when the app loads and the view contains a button to load UINavigationViewController.

Now I created a project with "Window-based Application" and added "RootViewController" in the project with .m, .h, and .xib.

Next I added a view and a button in the "RootViewController.xib" file and it runs ok. After that, I added "UIViewController subclass" file naming "NavViewController" with .h, .m and .xib files.

Also I added - (IBAction)buttonPressed:(id)sender function in the "RootViewController" classes to load NavigationViewController.

Here is the code of the "buttonPressed:".

- (IBAction)buttonPressed:(id)sender {
    NavViewController *navViewController = [[NavViewController alloc] initWithNibName:@"NavViewController" bundle:nil];
    self.navController = navViewController;
    [self.view insertSubview:navViewController.view atIndex:0];
    [navViewController release];
}

When I "build and go," it runs fine initially until I press the button. When I press button, program terminates it.

What am I doing wrong? Please help...

Thank you.

Upvotes: 0

Views: 1120

Answers (3)

Grouchal
Grouchal

Reputation: 9796

What are you doing wrong? Designing your app in a non-standard way - you are not supposed to be able to do this - the NavigationController is in charge!

Why would you have a button that then adds a navigation controller? - it goes against the user interface guidelines. I found it hard to get to grips with the interface guidelines to begin with but you really must because it will make your app so much more usable.

If you need a navigation controller then add it to the view to begin with - or create a new view with the navigation controller. Honestly try it out and you will feel the user interface feels much better.

If you really want a button that adds a navigation controller to the window then do the following:

  1. Keep a reference to the AppDelegate in your code
  2. Use this reference and pass in your current view controller to a method called reloadMainViewWithNavBar:(UNViewController*) viewController
  3. This new method should remove the old mainViewController and create a NavigationController using your viewController as the root view conroller
  4. add the navigation controller view to window view

Upvotes: 1

In addition to the fix mentioned above, what you really want to do is create the first view controller contained in the navigation controller - but hide the navigation bar until the button press causes it to unhide. You cannot easily add a navigation controller to a view you are in.

Upvotes: 0

Corey Floyd
Corey Floyd

Reputation: 25969

The navController has no view set in the nib. A UINavigationcontroller needs a root view. In the nib, connect the navigation controller to a another view controller.

Upvotes: 0

Related Questions