15

I created a new navigation controller in my storyboard (not programmatically!) and set it to be "Root View Controller" to a regular UIViewController and added a button in it which says- forward to the next view controller (this second view controller is a view controller which I want that will have a back button to link to the initial view controller). Now, whenever I try to link the button to the next view controller- "Pushing a navigation controller is not supported".

Help me please, and thanks

EDIT: I accidentally subclassed UINavigationController, and not UIViewController in my class. Thank you anyway.

Lior Pollak
  • 3,362
  • 5
  • 27
  • 48

4 Answers4

10

I've tried this and have no problems, its all done in IB with no additional code required ...

  1. Start a new project, "Single View Application" using story boards
  2. Select storyboard and delete the views its produced.
  3. Drag on a new Navigation Controller (it will bring a table view with it)
  4. Delete the table and the table view controller, so you are just left with the Navigation Controller
  5. Drag on a normal view controller
  6. Right Click and drag from the Navigation controller to the new View and choose "Relationship - Root View Controller"
  7. Drag a "Bar Button Item" on to the Navbar which should be visible on the top of your ViewController, you can rename this Forward if you wish.
  8. Now drag on another view controller which is the one your "Forward" button will push in to view.
  9. Right Click and drag from the bar button to the 2nd View Controller, and choose "Push"

Run the project and you will get a Single view with a Navbar and your button, clicking your button will Push the other view and give you a Back Button to return to the first View Controller. I'll try and post a picture of my storyboard if it helps.

PushViewControllers

Plasma

Plasma
  • 2,622
  • 2
  • 20
  • 35
  • Thanks! another (tiny) que- can I do this "push" programatically? I mean, I want to do an if statement- for instance, only if the app has been authorized to game center. How can I do that? – Lior Pollak May 30 '12 at 16:50
  • BTW- your way throws me this error: TwentyOneQuestions[37746:1bb03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported' *** First throw call stack: (0x132f022 0x26e6cd6 0x38cf1b 0xb36452d 0x38ca24 0x6e8de6 0x6dd4d0 0x1330e99 0x2b514e 0x4f3a0e 0x1330e99 0x2b514e 0x2b50e6 0x35bade 0x35bfa7 0x35b266 0x2da3c0 0x2da5e6 0x2c0dc4 0x2b4634 0x18e9ef5 0x1303195 0x1267ff2 0x12668da 0x1265d84 0x1265c9b 0x18e87d8 0x18e888a 0x2b2626 0x296d 0x28d5) terminate called throwing an exception(lldb) – Lior Pollak May 30 '12 at 16:59
  • I can only think you maybe did something wrong? Did you select a plain "view controller" for the last 2 views, and not the Navigation controller. up to yhr point o right click and drag the views should be plain white squares with nothing in them other than a status bar. The blue navigation bar is placed automatically once you connect up the boxes. – Plasma May 30 '12 at 20:41
  • Interesting, did you download my sample project in the post below and try that? – Plasma May 31 '12 at 14:11
  • If I do the modal transitioning, it works but displays a black view controller, with nothing in it- but it should display with white background and a few buttons. – Lior Pollak May 31 '12 at 18:00
  • In my sample project below I swapped the transition to Modal and it all still worked there's just no way to get back, you would need to use ... [self dismissModalViewControllerAnimated:YES]; if you want to display it modal. Or place another segue to get back to the original view controller. – Plasma Jun 01 '12 at 10:24
  • Figured it out- deleted the storyboard, created a new, works perfect. Thanks anyway! – Lior Pollak Jun 01 '12 at 12:59
1

I had the same trouble. I wanted to have a navigation controller on each storyboard, so that each could run independently, be individually debugged, and so that the look would be right with the navigation bar.

Using other approaches, I found the UINavigationController would be retained from the original storyboard -- which I didn't want -- or I'd get errors.

Using the AppDelegate in your view controller to set the rootViewController worked for me (borrowing segue naming conventions from Segue to another storyboard?):

- (void)showStartupNavigationController {
NSLog(@"-- Loading storyboard --");

//Get the storyboard from the main bundle.
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Startup" bundle:nil];
//The navigation controller, not the view controller, is marked as the initial scene.
UINavigationController *theInitialViewController = [storyBoard instantiateInitialViewController];

NSLog(@"-- Loading storyboard -- Nav controller: %@", theInitialViewController);

//Remove the current navigation controller.
[self.navigationController.view removeFromSuperview];
UIWindow *window = [(AppDelegate *)[[UIApplication sharedApplication] delegate] window];
window.rootViewController = theInitialViewController;
Community
  • 1
  • 1
LordParsley
  • 3,808
  • 2
  • 30
  • 41
  • I can possibly help you with this one. Consider http://stackoverflow.com/a/23403979/294884 and http://stackoverflow.com/a/23060746/294884 (it's always hard to know "just what is the problem/solution" with friction' storyboard-viewcontrollers-containers! If it's irrelevant to your needs here, no harm done.) – Fattie May 30 '14 at 14:38
0

To swap views Programatically you would need to select the segue and give it an Identifier like "PushView" then call it like this ....

[self performSegueWithIdentifier:@"PushView" sender:self];

That will programatically do the same as clicking the forward button. I've created you an example project with the code discussed above. Has an -(IBAction) with code in you can use for programatially changing the view.

PushView.zip

Plasma
  • 2,622
  • 2
  • 20
  • 35
0

I also wanted to do this, present a screen (that had an embedded navigation controller) when the user pushes a button.

At my first attempt, I connected the segue from the button in the fist screen to the Navigation Controller, and the app was crashing with this error "Pushing a navigation controller is not supported".

This is the solution I found:

Select the segue from the button in the first screen to the navigation controller. If it had an identifier, copy its name. Then delete that segue.

Then create a new segue by CTRL-clicking the button in the first view controller and dragging to the VIEW CONTROLLER YOU WANT TO PRESENT (not to the Navigation Controller that is pointing at it), and select Push in the small pop up window.

Then click the icon in the middle of the segue and paste the name you copied in the first step as an identifier for it.

IB is going to give you a warning "Scene is unreachable due to lack of entry points and does not have an identifier for runtime access via -instantiateViewControllerWithIdentifier:." Don't worry, it works perfectly.

If you want to customize the string that is shown as the Back button to return, you can add this line in the viewDidLoad method OF THE VIEW CONTROLLER THAT IS BEING SHOWED AFTER THE BUTTON IS PRESSED, that is the Child view controller.
(replace "Settings" with the name you need)

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBar.topItem.title = @"Settings";
    ...

}
alvaro
  • 2,676
  • 2
  • 20
  • 19