Sunday, May 1, 2011

Set a navigation controller without an app delegate

I would like to show a Navigation Controller after clicking a button. Every tutorial assumes the navigation controller will be the first screen so it links it to the App Delegate, but App delegate only appears at MainWindow.xib. How do you guys add a navigation controller to a view different than the MainWindow?

Thanks!

From stackoverflow
  • UINavigationController is to navigate a heirarchy of views with UIViewControllers. If you don't have a root UIViewController, it won't work (and doesn;t make sense). If you do have a UIViewController, you simply send a - (id)initWithRootViewController:(UIViewController *)rootViewController init message to a new navigation controller passing in your UIViewController.

  • Here is some sample code to expand on Roger's answer. The following method is linked to some user interaction on the current view controller (to compose an email for example). This will give the compose view the navigation bar across the top instead of coding buttons inside your custom view.

    -(void) composeButtonPushed: (id) sender {
        ComposeViewController *controller = [[ComposeViewController alloc] initWithNibName:@"ComposeView" bundle:nil];
        UINavigationController *composeNavController = [[UINavigationController alloc] initWithRootViewController:controller];
    [self presentModalViewController:composeNavController animated:NO];
    }
    

0 comments:

Post a Comment