PresentModalViewController is not working

here is 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 will happen. 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 looks just fine (no transition, of course). What am I doing wrong? Is there anything special you should take care of when initializing the view controller? I tried to copy as many of the Apple examples as possible, but I can't get this to work ...

Thanks for any input!

- Ry

+6
iphone uiviewcontroller
source share
2 answers

You can only imagine modal view controllers from controllers that have already been shown on the screen (usually through the UINavigationController or UITabBarController). Try creating a UINavigationController by clicking on the viewController and then presenting your modal controller. There, the initial project in Xcode shows how to create a stream based on the UINavigationController if you are not familiar with it.

One more note: if you did not push the view controller onto the UINavigationController, the .navigationController property will be zero, and messaging will have no effect.

+24
source share

I ran into the same problem trying to show a modal view of another modal view. Ben's answer is correct and can be implemented like this:

 @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]; } 
+1
source share

All Articles