6

here's my code:

ViewController *vc = [[ViewController alloc] initWithNibName:@"TableView" bundle:nil];
[self.navigationController presentModalViewController:vc animated:YES];
//[self setView:[vc view]];

If I call it, nothing happens. However, if I change it to:

ViewController *vc = [[ViewController alloc] initWithNibName:@"TableView" bundle:nil];
//[self.navigationController presentModalViewController:vc animated:YES];
[self setView:[vc view]];

The view appears just fine (without the transition, of course). What am I doing wrong? Is there anything special you have to take care of when initializing the view controller? I tried to copy as much as possible from Apple's examples, but I can't get this to work...

Thanks for any input!

-- Ry

ryyst
  • 9,563
  • 18
  • 70
  • 97

2 Answers2

24

You can only present modal view controllers from controllers that have already been shown onscreen (usually through a UINavigationController or UITabBarController). Try creating a UINavigationController, pushing a viewController to it, and then presenting your modal controller. There's a starter project in Xcode that shows how to create a UINavigationController-based flow if you're unfamiliar with it.

One other thing to note: if you haven't pushed the view controller onto a UINavigationController, the .navigationController property will be nil, and messaging it will have no effect.

Ben Gottlieb
  • 85,404
  • 22
  • 176
  • 172
  • Meh. Isn't there an easier way to do this? I don't really want to create a UINavigationController just to be able to slide up a view... Thanks for your answer, though! – ryyst Mar 21 '10 at 15:52
  • You don't have to create a nav controller to do this, but you're referencing it here. If just have a view controller, you can just use IT to present a modal view controller (ie, get rid of the ".navigationController" if your code example above. You will have to add your viewController's view to your main window to make this work. – Ben Gottlieb Mar 21 '10 at 21:08
  • 13
    Another thing for people browsing here: You might need to move code for presenting a VC from viewDidLoad: to viewDidAppear:animated:. I presume this is because the view hasn't been shown onscreen in viewDidLoad. – Tom H May 31 '11 at 15:46
  • Hi @Ben Gottlieb and Tom H and all. I have a problem with UIModalViewController full screen in ipad. I need your help. This is my question can you help me? Please see this link to know my problem...http://stackoverflow.com/questions/6413263/uimodalpresentationfullscreen-not-working-in-ipad-landscape-mode – Yuvaraj.M Jun 22 '11 at 08:16
  • 3
    @TomH, thanks for that suggestion. It helped me a lot - indeed model view controller must be presented inside viewDidAppear, not viewDidLoad! – Matej Balantič Oct 26 '11 at 13:16
1

I encountered the same problem while attempting to show a modal view over another modal view. Ben's answer is correct, and can be implemented like so:

@interface FirstView: UIViewController {

    UIViewController *firstView;
}

- (IBAction)showOptionsView:(id)sender;

@end

In the main view class:

- (void)viewDidLoad {
    [super viewDidLoad];

    firstView = [[UIViewController alloc]init];
    [firstView setView:self.view];
    [firstView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
}

- (IBAction)showOptionsView:(id)sender {
    OptionsView *optView = [[OptionsView alloc]initWithNibName:@"OptionsView" bundle:nil];

    if(firstView != nil) {
        [firstView presentModalViewController:optView animated:YES];
        [optView release];
}
ninehundreds
  • 1,097
  • 2
  • 21
  • 43