2014-09-10 86 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]; 
} 
相關問題