9

How do I add an existing UIViewController (which is presented using presentModalViewController) to a UINavigationController?

When user tap on button, a new copy of my detail view need to be pushed. (In other words, pushViewController displaying pushViewController, modally, in a UINavigationController).

Easiest way to enable this functionality?

2
  • It's not clear what you're asking. Do you want to present a UINavigationController modally? That "feels" wrong but I don't know for sure. Commented Feb 10, 2011 at 14:01
  • that's the simplified description... presenting UINavigationController modally... (but modifying existing UIViewController) Commented Feb 10, 2011 at 14:11

2 Answers 2

37

how do you create your modal viewcontroller? Just wrap the controller into a UINavigationController

Let's assume your modal code is like this:

MyExampleViewController *vc = [[[MyExampleViewController alloc] initWithNibName:@"MyExample" bundle:nil] autorelease];
[self presentModalViewController:vc animated:YES];

Then change it into something like this:

MyExampleViewController *vc = [[[MyExampleViewController alloc] initWithNibName:@"MyExample" bundle:nil] autorelease];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:vc] autorelease];
[self presentModalViewController:navController animated:YES];
Sign up to request clarification or add additional context in comments.

5 Comments

there is MyExampleViewController and MyDetailViewController in the viewControllers property of the navController. But the screen is not updated (MyDetailViewController is not shown)?
how to call pushViewController in the MyExampleViewController?
[self.navigationController pushViewController:anotherVC animated:YES];?!
I use this method to.But i can't for the life of me add a UIBarButtonItem to the toolbar that comes with the navigationController. Any help would be appreciated?
and self.navigationItem.rightBarButtonItem = yourUIBarButtonItem; does not work? Btw, it's not a toolbar, it's a navigationBar. Maybe you should create a new question for your problem.
0

I think you need to add a navigation controller in your delegate, after that you can push the view. So that you can push the view from anywhere in your application.

on AppDelegate.h

UINavigationController *navig;

on AppDelegate.M

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    navig = [[UINavigationController alloc] initwithRootViewController:viewController.view];

    //[navig pushViewController:viewController animated:YES];
    [self.window addSubview:navig.view];
    [self.window makeKeyAndVisible];

    return YES;
}

3 Comments

I can't understand either the question or see the relevance of this answer :).
u want to push newdetailview controller, so u should have navigation controller then u can pus
If you will add a navigation controller in the application delegate, then only push the view from anywhere in the class. Because If your application is View Based Application, then the navigation controller doesn't works, so better you should add the navigation controller in the delegate, you can easily push the view. See my answer, i have edited.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.