Pushing a UIViewController from a modal view

By Greg Goltsov

The problem

While working on an iOS project last summer, I stumbled upon a very strange problem. Something that should have been very trivial in iOS views management, yet wasn't. The problem was this:

How to push a UIViewController from within a modal view.

Or, as someone else asking about it put it:

I want to display a view with push transition inside a view which was displayed with modal transition.

The solution

While googling around, I stumbled upon a great post basically solving the problem. However, I thought it would be nice to package the solution in a simple category for everyone to use. Find it here.

After these files are included in the relevant UINavigationController subclass, the modal controller should be created this way from within the root controller:

UINavigationController * modalNC = [[UINavigationController alloc] initWithRootViewController:rootVC];

// Very important to set the delegate here (see http://www.kickasslabs.com/2009/07/03/uinavigationcontroller-tricks/ )
modalNC.delegate = self;

// blah blah blah ...

The most important thing here is to set the delegate to self (it is the controller that will still be visible under the modal view).

Then, when it is time to push, just call

[self pushViewControllerFromModal:newVC animated:YES];

from within the modal navigation controller.