Push view controller into modal view controller view

joan2404 picture joan2404 · Oct 12, 2012 · Viewed 10.2k times · Source

I'm trying to get working a simple operation. At least it seems simple. Ok, what I'd like to do is to push a view (with push view controller) from a view that has been pushed with modal view controller.

View1 --(push using modal view controller)-->View2--(push using push view controller)--View3.

Rigth now, i'm doing tests so i'm using a button to start the action. Here's the code I use to push from View2 to view 3:

//view2.h
UIToolbar *bar;
UIBarButtonItem *button;
UIToolbar *toolbar;

}

- (IBAction)demissModal:(id)sender;
- (IBAction)goView3:(id)sender;

@end

//view2.m
- (IBAction)goView3:(id)sender{

View3 *view_3 = [[View3 alloc] initWithNibName:@"View3" bundle:nil];
[self.navigationController pushViewController:view_3 animated:YES];

}

This is the same code I use to push View1 to View2, and it works. But when pushing View2 to View3, it's not working. Any idea of why happens that? Thanks!

Answer

MaxGabriel picture MaxGabriel · Oct 12, 2012

View Controllers aren't actually 'modal' or 'push' view controllers. Modal or Push describe a transition between view controllers (called segues if you're using storyboards).

What I think you're asking is how to modally present a view controller, and then push another controller. The trick is when you modally present view controller #1, to actually present a navigation controller with its root view controller set as view controller #1.

MyViewController *myViewController = [MyViewController alloc] init];
UINavigationController *navController = [UINavigationController alloc] initWithRootViewController:myViewController];

// Presuming a view controller is asking for the modal transition in the first place.
[self presentViewController:navController animated:YES completion:nil];
// Now in myViewController, call [self.navigationController pushViewController:secondViewController animated:YES];

This is what it looks like using storyboards: enter image description here