xcode 尝试在转换时关闭演示控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25762466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:30:50  来源:igfitidea点击:

Trying to dismiss the presentation controller while transitioning already

iosxcode

提问by ylovesy

I compile my project using Xcode6 GM on iOS8 GM. When dismissing many view controllers, my app always crashes and the debug area shows:

我在 iOS8 GM 上使用 Xcode6 GM 编译我的项目。当关闭许多视图控制器时,我的应用程序总是崩溃并且调试区域显示:

"Trying to dismiss the presentation controller while transitioning already. transitionViewForCurrentTransition is not set, presentation controller was dismissed during the presentation? "

“在转换时尝试关闭演示控制器。transitionViewForCurrentTransition 未设置,演示控制器在演示期间被关闭?”

I have googled and find a similar case and shows the same error:

我用谷歌搜索并找到了一个类似的案例并显示了相同的错误:

[self.viewController presentViewController:vc animated:NO completion:^{
        [self.viewController dismissViewControllerAnimated:NO completion:nil];
}];

It works fine using Xcode5 and iOS7 . What does the error means? Is iOS8 isn't happy with the "Hack"? Thanks in advance.

使用 Xcode5 和 iOS7 可以正常工作。错误是什么意思?iOS8 对“黑客”不满意吗?提前致谢。

采纳答案by ylovesy

My solution:

我的解决方案:

dismissViewControllerAnimated:completion:If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack.

DismissViewControllerAnimated:completion:如果您连续呈现多个视图控制器,从而构建呈现的视图控制器的堆栈,则在堆栈中较低的视图控制器上调用此方法会解除其直接子视图控制器以及堆栈中该子视图之上的所有视图控制器。发生这种情况时,只有最顶层的视图以动画方式消失;任何中间视图控制器都被简单地从堆栈中删除。

For example,I have 4 views:A->B->C->D and when I want to dismiss B, I firstly check if C also want to dismiss by using objc_setAssociatedObject to attach/detach a NSString object, and if C wants to dismiss too,then just cancel C's request.Just call dismiss to B.

例如,我有 4 个视图:A->B->C->D,当我想关闭 B 时,我首先检查 C 是否也想通过使用 objc_setAssociatedObject 附加/分离 NSString 对象来关闭,如果 C 想要也解雇,然后取消 C 的请求。只需向 B 调用解雇。

回答by olivier

Are you trying to force a device orientation change? Anyway, in my opinion you could try to change your current code to:

您是否试图强制更改设备方向?无论如何,在我看来,您可以尝试将当前代码更改为:

[self.navigationController presentViewController:vc animated:NO completion:^{
    dispatch_after(0, dispatch_get_main_queue(), ^{
        [self.navigationController dismissViewControllerAnimated:NO completion:nil];
    });
}];

回答by Gabriele

I had the same issue and I found a clean solution avoid using dispatch_async or dispatch_after.

我遇到了同样的问题,我找到了一个干净的解决方案,避免使用 dispatch_async 或 dispatch_after。

Simply, as described by the exception, you are trying to dismiss a view controller while the presenting transition is still in progress. This means that once the

简单地说,如异常所描述的,您正在尝试在呈现转换仍在进行中时关闭视图控制器。 这意味着一旦

- presentViewController:animated:completion: 

completion block is called, and you invoke the dismiss, the transitioning is not completed.

完成块被调用,并且你调用了dismiss,转换没有完成。

Starting from iOS 7 transitioning UIViewController has a new method available

从 iOS 7 开始过渡 UIViewController 有一个可用的新方法

- transitionCoordinator 

The transitionCoordinator gives you the chance to enqueue a completion block as soon as the transition completes.

transitionCoordinator 使您有机会在转换完成后立即将完成块排入队列。

The object returned by the method conforms the UIViewControllerTransitionCoordinator protocol. Knowing that the solution is really simple.

方法返回的对象符合 UIViewControllerTransitionCoordinator 协议。知道解决方案真的很简单。

After the invocation of

调用后

- presentViewController:animated:completion: 

the transition coordinator is properly configured by the framework.

框架正确配置了转换协调器。

Use

- animateAlongsideTransition:completion: 

on it to send the proper completion block.

在它上面发送正确的完成块。

Here a little code snippet that better explain the solution

这里有一个小代码片段,可以更好地解释解决方案

void(^completion)() = ^() {
    [modalViewController dismissViewControllerAnimated:YES completion:nil];
};

// This check is needed if you need to support iOS version older than 7.0
BOOL canUseTransitionCoordinator = [viewController respondsToSelector:@selector(transitionCoordinator)];

if (animated && canUseTransitionCoordinator)
{
    [viewController presentViewController:modalViewController animated:animated completion:nil];
    [viewController.transitionCoordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        completion();
    }];
}
else
{
    [viewController presentViewController:modalViewController animated:animated completion:completion];
}

回答by Jr.Mob Application Developer

ThirdViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"Third"];
UIViewController *VC1 = self.presentingViewController;
[self dismissViewControllerAnimated:NO completion:^{}];
[VC1 presentViewController:vc animated:YES completion:^{}];