-dismissModalViewControllerAnimated:适用于iPhone而非iPad

-dismissModalViewControllerAnimated:适用于iPhone而非iPad,iphone,objective-c,modalviewcontroller,Iphone,Objective C,Modalviewcontroller,我很难让-dismissModalViewControllerAnimated:在iPad上工作(作为iPhone应用程序)。出于某种原因,它似乎什么也没做 我调用-presentModalViewController:animated:在MainViewController中,之后我尝试从显示的视图控制器调用-dismissModalViewController(使用[self dismissModalViewController]),我理解该控制器会将请求转发给MainViewControl

我很难让-dismissModalViewControllerAnimated:在iPad上工作(作为iPhone应用程序)。出于某种原因,它似乎什么也没做

我调用-presentModalViewController:animated:在MainViewController中,之后我尝试从显示的视图控制器调用-dismissModalViewController(使用[self dismissModalViewController]),我理解该控制器会将请求转发给MainViewController。我还尝试在显示的视图控制器中设置代理(viewControllerAboutToBePresented.delegate=self;),然后调用[self.delegate dismissModalViewController:YES]。当我在iPad上运行iPhone应用程序时,这两种方法似乎都不起作用


我怎样才能关闭iPad上的模态视图控制器呢?

我想将此作为评论发布,但我无法这样做

是应调用dismissModelViewControllerAnimated的主视图控制器:。您可以在显示的视图控制器中调用[[self-parentViewController]dismissModalViewControllerAnimated:],也可以在协议中定义一个方法,该方法将关闭模式视图控制器并在主视图控制器中实现该协议,将其设置为显示的视图控制器的委托,然后从中调用该方法。你做得不对。它可能会也可能不会解决您的问题

更新(注释中没有代码示例):

在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];
}
-(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.
}
而在作为模态视图控制器显示的局部视图控制器上,唯一需要的是这样的内容

- (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];
}
-(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.
}

但是,如果应用程序在iPhone上运行良好,那么它在iPad上的运行应该与iPhone应用程序一样好。

我想将此作为评论发布,但我无法这样做

是应调用dismissModelViewControllerAnimated的主视图控制器:。您可以在显示的视图控制器中调用[[self-parentViewController]dismissModalViewControllerAnimated:],也可以在协议中定义一个方法,该方法将关闭模式视图控制器并在主视图控制器中实现该协议,将其设置为显示的视图控制器的委托,然后从中调用该方法。你做得不对。它可能会也可能不会解决您的问题

更新(注释中没有代码示例):

在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];
}
-(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.
}
而在作为模态视图控制器显示的局部视图控制器上,唯一需要的是这样的内容

- (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];
}
-(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.
}

但是,如果应用程序在iPhone上运行良好,那么它在iPad上的运行应该与iPhone应用程序一样好。

我第一次将iPhone项目移植到iPad上时就有了这一点——这是
[self dismissModalViewControllerAnimated::
悄然失败的。该项目在OpenGL背景上使用Cocoa视图控制器,我认为这与此有关


由于截止日期非常紧迫,我没有时间去弄清楚到底发生了什么,所以我只是添加了模态视图作为当前视图控制器的子视图,并在完成后将其删除。(是的,一个黑客,但这是你的时间尺度…

我第一次将iPhone项目移植到iPad上时就遇到了这个问题-正是
[self dismissModalViewControllerAnimated::
悄悄地失败了。该项目在OpenGL背景上使用Cocoa视图控制器,我认为这与此有关


由于截止日期非常紧迫,我没有时间去弄清楚到底发生了什么,所以我只是添加了模态视图作为当前视图控制器的子视图,并在完成后将其删除。(是的,一个黑客,但这是你的时间尺度…

你对代码的描述看起来很正确。你有一个失败的最小测试用例吗?如果你是说一个项目就是为了测试这个,不,我没有设置任何类似的东西。但是,我已经验证了在父视图控制器中调用了-dismissModalViewController,并且没有任何数量的-dismissModalViewController调用会导致视图控制器被解除。我会抓取我使用的实际代码,并在有机会的时候将其发布在上面-无论如何我应该这样做。你对代码的描述看起来很正确。你有一个失败的最小测试用例吗?如果你是说一个项目就是为了测试这个,不,我没有设置任何类似的东西。但是,我已经验证了在父视图控制器中调用了-dismissModalViewController,并且没有任何数量的-dismissModalViewController调用会导致视图控制器被解除。我会抓取我使用的实际代码,并在有机会的时候将其发布在上面-无论如何我应该这样做。谢谢你的回复,sigsegv。我可能错了,但我非常确定[self-parentViewController]将是调用-presentModalViewController的对象,我使用self.delegate调用了该对象。无论如何,正如您所描述的,我也使用[self-parentViewController]进行了尝试。你有幸在iPad上的iPhone应用程序中看到这条消息吗?(我还没有在为iPad设计的应用程序中使用模态视图控制器)我不记得在iPad上运行iPhone应用程序时模态视图控制器有任何问题。我手头没有iPad,但上面帖子中更新的代码在iPad模拟器上运行的iPhone专用应用程序中运行良好。谢谢你的回复,sigsegv。我可能错了,但我非常确定[self-parentViewController]将是调用-presentModalViewController的对象,我使用self.delegate调用了该对象。无论如何,正如您所描述的,我也使用[self-parentViewController]进行了尝试。你有幸在iPad上的iPhone应用程序中看到这条消息吗?(我还没有在为iPad设计的应用程序中使用模态视图控制器)我不记得在iPad上运行iPhone应用程序时模态视图控制器有任何问题。我在h没有iPad