views:

749

answers:

1

Hi,

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

+3  A: 

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
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!
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