开发者

presentModalViewController not working

开发者 https://www.devze.com 2022-12-23 10:20 出处:网络
here\'s my code: ViewController *vc = [[ViewController alloc] initWithNibName:@\"TableView\" bundle:nil];

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


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.


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];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号