How to add navigation controller programmatically?

13,859
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[[NewViewController alloc] initWithNibName:@"NewViewController" bundle:nil]];

[self presentModalViewController:navigationController               animated:YES];
            [navigationController release];

And in NewViewController: USe this to dismiss and get back to previous view.

[[self navigationController] dismissModalViewControllerAnimated:YES];
Share:
13,859
CKT
Author by

CKT

Senior iOS application developer

Updated on June 05, 2022

Comments

  • CKT
    CKT almost 2 years

    In my app there is requirement that..I have 6 buttons in a nib, when I press any button a new nib will be loaded into the window according to the button pressed. problem is after loading the new nib If I want to come back to the previous nib (which is having all the buttons) how to add navigation controller?

    what I am doing now is while loading the new nib when I pressed the button

    objNewViewController = [[NewViewController alloc] initWithNibName:@"NewViewController" bundle:nil];
    [self.navigationController pushViewController:objNewViewController animated:YES];
    

    but by this way im not able to load the nib, it's not performing any operation?

  • CKT
    CKT about 14 years
    thank you for the reply johan.. but the thing is the nib which is having these 5 to 6 buttons is not the first nib.. i have 2 nibs before this..after that this buttons nib will come. here i cant take the navigation controller especially rite? that why i wanna add the navigation controller programatically atleast to come back till the nib which contains the buttons.
  • Johan Kool
    Johan Kool about 14 years
    In that case, create the UINavigationController using its designated initializer (-init...) and add its view when you need it. You can the push other viewcontrollers on its stack after you've created them with -initWithNib...
  • CKT
    CKT about 14 years
    when I do this it is loading my newViewController's nib, but the problem is it doesnt have any back button to come back. how to solve this?
  • Manjunath
    Manjunath about 14 years
    you can add leftBarButton to navigation item. Add cancel button and in its action method, dismiss the model.
  • Mayank
    Mayank over 10 years
    presentModalViewController is deprecated from iOS 6.0. What can be the alternative of this.