How do I add a UINavigationController to a view in code?

22,383

Solution 1

The answer for this question is here: Having problem with pushViewController!! Help

Solution 2

The way to link a view controller with a navigation controller is to push the view controller onto the navigation stack. For example:

UIViewController * yourViewController = [[UIViewController alloc] init];
UINavigationController * navigation = [[UINavigationController alloc] init];
[navigation pushViewController:yourViewController animated:NO];
[yourViewController release]

Finally release the view controller at the end since the navigation controller retains it.

Share:
22,383
Thang Pham
Author by

Thang Pham

Updated on March 01, 2020

Comments

  • Thang Pham
    Thang Pham about 4 years
    view1 = [[View1 alloc] init];   //Create the first view
    UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:view1];
    navigationController1.navigationBar.tintColor =[UIColor blackColor];
    

    View1 is inherit from UIViewController. So I create a *view1, then I create a UINavigationController, call *navigationController1. How do I link the two together? Thank you very much

  • Luke Mcneice
    Luke Mcneice over 13 years
    You can init with the root view: UINavigationController * navigation = [[UINavigationController alloc] initWithRootViewController: yourViewController];