Search code examples
iosobjective-cuinavigationcontrolleruinavigationbaruinavigationitem

Create a UINavigationController For present 2 UIViewController in Objective-C


I have 2 UIViewControllers and want to have UINavigationController with UINavigationBar & UINavigationItem on them. But my code doesn't working ..

here is my code :

#import "testView1.h"
#import "testView2.h"

@interface testView1 ()

@end

@implementation testView1

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor darkGrayColor];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self];
    testView2 *detail = [testView2 new];
    [navController pushViewController:detail animated:YES];
}

Solution

  • Try to embed navigation controller in storyboard as below :

    First select your testView1 in storyboard .

    Select Navigation Controller

    enter image description here

    And Changes as below

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    
       /// testView2 *detail = [testView2 new];
        testView2 *detail =  [self.storyboard instantiateViewControllerWithIdentifier:@"testView2 Identifier"]; // if you have add controller in storyboard
        [self.navigationController pushViewController:detail animated:YES];
    }