简体   繁体   English

-dismissModalViewControllerAnimated:在iPhone而非iPad上运行

[英]-dismissModalViewControllerAnimated: works on iPhone not iPad

I'm having trouble with getting the -dismissModalViewControllerAnimated: to work on the iPad (as an iPhone app). 我在使-dismissModalViewControllerAnimated:在iPad(作为iPhone应用程序)上工作时遇到麻烦。 For some reason it doesn't seem to do anything. 由于某种原因,它似乎什么也没做。

I call -presentModalViewController:animated: in MainViewController, after which I've tried calling the -dismissModalViewController from the presented view controller (using [self dismissModalViewController] ), which I understand forwards the request to the MainViewController. 我在MainViewController中调用-presentModalViewController:animated : ,然后尝试从呈现的视图控制器(使用[self dismissModalViewController] )中调用-dismissModalViewController ,据我了解,该请求会将请求转发给MainViewController。 I've also tried setting a delegate in the presented view controller ( viewControllerAboutToBePresented.delegate = self; ), and then calling [self.delegate dismissModalViewController:YES] . 我也尝试过在显示的视图控制器( viewControllerAboutToBePresented.delegate = self; )中设置一个委托,然后调用[self.delegate dismissModalViewController:YES] Neither approach seems to do anything when I run the iPhone app on the iPad. 当我在iPad上运行iPhone应用程序时,这两种方法似乎都不起作用。

How can I dismiss the modal view controller on the iPad? 如何在iPad上关闭模态视图控制器?

I had this the first time I ported an iPhone project to the iPad - it was [self dismissModalViewControllerAnimated:] that was quietly failing. 我第一次将iPhone项目移植到iPad时遇到了这个问题- [self dismissModalViewControllerAnimated:]悄然失败了。 The project was using Cocoa view controllers over an OpenGL background, and I thought it was something to do with that. 该项目在OpenGL背景上使用了Cocoa视图控制器,我认为这与此有关。

Because of the ridiculously tight deadline, I had no time to work out what was going on, and so I just added the modal view as a subview of the current view controller, and removed it when I was done. 由于截止日期太短了,我没有时间弄清楚发生了什么,所以我只是将模式视图添加为当前视图控制器的子视图,并在完成后将其删除。 (Yeah, a hack, but that's timescales for ya...) (是的,是hack,但这是您的时间表...)

I woud post this as a comment, but I'm unable to do so. 我会将其发布为评论,但我无法这样做。

Is the main view controller that should call dismissModelViewControllerAnimated: . 是应调用dismissModelViewControllerAnimated:的主视图控制器。 You can either call [[self parentViewController]dismissModalViewControllerAnimated:] in the presented view controller or define a method in a protocol that dismiss the modal view controller and implement the protocol in the main view controller, set it as the delegate of the presented view controller and call the method from it. 您可以在显示的视图控制器中调用[[self parentViewController] dismissModalViewControllerAnimated:] ,也可以在协议中定义一个方法以关闭模态视图控制器并在主视图控制器中实现该协议,将其设置为显示的视图控制器的委托并从中调用方法。 You are doing it the wrong way. 您做错了方法。 It might or might not solve your issue. 它可能会或可能不会解决您的问题。

Update (code sample isn't available on comments): 更新(注释中不提供代码示例):

On the MainViewController you should have something like this 在MainViewController上,您应该有这样的内容

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // In this case is inside a tableviewmethod, but it could really be an action associated with a button.

    DetailViewController *controller = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];
    [controller setDelegate:self]; // The delegate is the parent and is assigned, not retained.

    // Modal presentation style is only used on iPad. On iPhone is always full screen.
    // [controller setModalPresentationStyle:UIModalPresentationFullScreen];

    [controller setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentModalViewController:controller animated:YES];
    [controller release]; // It will be deallocated upon dismissal.
}

-(void)dismissDetailViewControllerAndProcessData:(NSDictionary *)data {

    // Do something with the data passed.
    [self processData:data];

    // Dismiss the modalviewcontroller associated with this viewcontroller.
    [self dismissModalViewControllerAnimated:YES];
}

while on the detail view controller presented as modal view controller, the only thing needed is something like this 在作为模态视图控制器呈现的局部视图控制器上,唯一需要的是这样的东西

-(void)actionBack:(id)sender {

    // Call the delegate method. If you just need to dimiss the controller, just
    // call
    // [[self parentViewController]dismissModalViewControllerAnimated:YES];
    // and don't even bother to set up a delegate and a delegate method.

    [delegate dismissDetailViewControllerAndProcessData:nil]; // Call the parent dismissal method.
}

but if the application is running fine on iPhone, it should run just as fine on the iPad as iPhone app. 但是,如果该应用程序在iPhone上运行良好,则应在iPad上与iPhone应用程序一样运行良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM