1

I have a standard UIViewController which works a charm. However, I want to add a UINavigationController on top of this as a subview, add and remove it at will. However, it seems you can't simply add another controller onto a current controller as a subview. So how do I go about this?

Thanks.

bryanmac
  • 38,941
  • 11
  • 91
  • 99
Josh Kahane
  • 16,765
  • 45
  • 140
  • 253
  • 1
    What do you mean when you say "as a subview"? You want part of your original UIViewController visible, as well as the UINavigationController? Apple warn against doing this sort of thing. You *can* do something like this -- but I'd question what you're trying to achieve before worrying about how it might be done. – occulus Oct 19 '11 at 01:02
  • What are you targetting here? iPad or iPhone? – occulus Oct 19 '11 at 01:03
  • Currently just iPhone, but iPad eventually. I see what you mean, I referred to adding a subview as just an ordinary method of adding an object to my view to be seen. However, I don't need to see the original view when I pull up this new nav controller, what would you suggest? – Josh Kahane Oct 19 '11 at 01:06
  • 1
    I don't see the code, but shouldn't it be the other way around? Have a navigation controller, then push your current working view controller onto the stack. You can still control the NavigationController's toolbar and navbar from your ViewController using [self parentViewController]. – Peter Sarnowski Oct 19 '11 at 01:09
  • @PeterSarnowski, I know what you are thinking, however my app revolves around a standard view controller and I need a nav controller to carry out a small portion of functionality and pop up at any chosen time. Or are you suggesting having the nab controller the base of it, then have the view controller on top then just show the nab controller when required? – Josh Kahane Oct 19 '11 at 01:12
  • 1
    @Josh Kahane: Exactly! You can have your current view controller set up as root controller of the navcontroller - and hide all nav features - the user experience will not change in the slightest! Your view controller will handle everything as before - and when you need the nav functions, you can bring up the toolbar or navbar with built in animations - it should work and look pretty good. At least i do it this way in my apps ;) – Peter Sarnowski Oct 24 '11 at 02:22

2 Answers2

1

Here's some code that creates a new view in a navigation controller and shows it "on top" (presents it modally).

There's a few key things here:

If you present a nav controller modally, you need to set it's left & right buttons (if you need to) before you initWithRootController and presentModally

Even if you're current view is in a navController, if you present it modally, it needs to be wrapped in a UINavigationcontroller (there's some SO posts covering that)

UINavigationController with presentModalViewController

MyView *myView = [[MyView alloc] initWithNibName:@"MyView" bundle:nil];

UIBarButtonItem *cancelBtn = [[UIBarButtonImageItem alloc] init...  

UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] init...

[[myView navigationItem] setLeftBarButtonItem:cancelBtn];
[[myView navigationItem] setRightBarButtonItem:doneBtn];  
[cancelBtn release];    
[doneBtn release];

// Edit purchase in full modal view.
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myView];

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

Then, from within the view you just presented modally, you can dismiss it. For example, in this code the save and cancel buttons added above are associated with these IBAction methods on the view controller you presentedModally:

- (IBAction)cancel:(id)sender
{
    NSLog(@"cancel");
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)save:(id)save
{
    NSLog(@"done");
    // do work here
    [self dismissModalViewControllerAnimated:YES];
}
Community
  • 1
  • 1
bryanmac
  • 38,941
  • 11
  • 91
  • 99
0

You can add another's controllers view as subview of that viewcontroller's view

[mainViewController addSubview:anotherViewController.view];

In the case of a UINavigationController though, although I don't know what you are doing, that is not ordinarily something that you would want to do.

Have you looked at -(void)presentModalViewController:animated?

Matthew Gillingham
  • 3,429
  • 1
  • 22
  • 26