ios - 连续调用 UIViewController 的 presentViewController 方法

标签 ios asynchronous presentmodalviewcontroller

我最近在我的 iOS 应用程序中遇到了一个令人毛骨悚然的情况,我试图从我的窗口的 rootViewController 中连续关闭一个呈现的 UIViewController,使用:

[rootViewController dismissViewControllerAnimated:YES completion:NULL]

并在不久之后展示另一个(顺便说一下,以另一种方法),其中:

UIViewController *vc2 = [[[MyViewController2 alloc] initWithNibName:nil bundle:nil] autorelease];
[rootViewController presentViewController:vc2 animated:YES completion:NULL];

问题是,我永远无法让第二个 View Controller 显示出来。事实证明,据我所知,dismissViewControllerAnimated:completion: 需要“完成”时间的异步 block 才能通过,然后才能正常工作 presentViewController:animated:completion:再次。 Apple's docs 中没有直接记录这一事实,据我所知。

我想出的解决方案是用一个方法包装解雇,该方法指定您之后要调用的选择器,如下所示:

- (void)dismissViewController:(UIViewController *)presentingController
                   postAction:(SEL)postDismissalAction
{
    [presentingController dismissViewControllerAnimated:YES
                                             completion:^{
                                                            [self performSelectorOnMainThread:postDismissalAction
                                                                                   withObject:nil
                                                                                waitUntilDone:NO];
                                                         }];
}

然后我会调用:

[self dismissViewController:self.window.rootViewController
                 postAction:@selector(methodForNextModalPresentation)];

无论如何,我想发帖,因为我环顾四周并没有看到任何人有这个特殊问题,所以我认为它可能对人们理解有用。而且,我想验证我没有破解具有更好的解决方案设计模式的解决方案。

最佳答案

只是为了清楚起见。你是说这段代码不起作用吗?

[myRootViewController dismissViewControllerAnimated:YES completion:^{
    [myRootViewController pushViewController:newController animated:YES];
}];

关于ios - 连续调用 UIViewController 的 presentViewController 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10629080/

相关文章:

ipad - 调整 ModalViewController 的大小并将其放置在 iOS 7 的中心

ios - 如何在用户无法看到呈现 View 的情况下使用已经呈现的模态视图 Controller 启动 iOS 应用程序?

ios - 为 objective-c 中的不同 View Controller 提供通用功能

ios - Firebase 按日期对数据排序

ios - UITable 滚动不流畅,单元格在 IOS 中显示错误信息

java - Vertx future

javascript - while 循环中的异步方法与 Graph API 分页

iOS 如何在已经堆叠的 ViewController 上调用 presentViewController

ios - 用户选择的 swift 3 重复间隔内的本地通知

ios - 将 SkeneKit 场景捕获为视频