2014-09-10 85 views
30

我使用iOS8 GM上的Xcode6 GM编译我的项目。当解雇许多视图控制器,我的应用程序总是崩溃和调试区域显示:尝试在已经转换时关闭演示控制器

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

我用Google搜索,发现了类似的情况,并显示了同样的错误:

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

它工作正常使用Xcode5和IOS 7 。错误的含义是什么? iOS8对“Hack”不满意吗?提前致谢。

+0

什么是你想上面的代码来实现的? – 2014-09-10 12:26:53

+0

我刚刚遇到了完全相同的错误.. – Jeef 2014-09-10 15:07:48

+0

我的假设是强制一个视图到一个单一的方向 - 这就是我在做什么,我有同样的错误 – Jeef 2014-09-10 15:16:25

回答

1

我的解决办法:

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.

例如,我有4个观点:A-> B-> C-> D,当我想解雇B时,我首先检查C是否也想通过使用objc_setAssociatedObject附加/分离NSString对象来解雇,如果C也想解雇,那么只需取消C的请求。只需致电驳回B.

36

您是否试图强制改变设备方向? 总之,在我看来,你可以尝试在当前的代码更改为:

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

您可以简单地派发主队列没有零延迟。 – malex 2014-09-22 14:56:56

+0

非常感谢你!这对我有用 – sogwiz 2014-09-25 20:28:31

+0

有关这方面的消息吗?这是一个非常丑陋的黑客攻击,尤其是如果你在多个类中有类似的实现! – 2014-10-08 17:06:15

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

你可以请你的答案添加一些颜色。这不会帮助任何人。 – Mika 2015-06-16 16:17:29

+0

对不起,我没有得到你。 @Mika – 2015-06-17 07:07:36

+0

你只需要放几行代码。您能否详细说明该代码的作用以及它为什么是解决方案。 – Mika 2015-06-17 07:20:02

5

我有同样的问题,我发现一个干净的解决方案避免使用dispatch_async或dispatch_after。

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

- presentViewController:animated:completion: 

完成块被调用,调用该辞退的转变尚未完成。

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

- transitionCoordinator 

的transitionCoordinator给你 机会尽快转换完成后排队完成块。

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

- presentViewController:animated:completion: 

过渡协调器的调用之后被适当地由框架构成。

使用

- animateAlongsideTransition:completion: 

它发送正确完成块。

这里有点代码段更好地解释解决方案

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]; 
} 
相关问题