[英]Error when dismissing view controller
解雇視圖控制器時出錯,之前沒有看到類似的東西,互聯網上沒有太多關於它的信息。
繼承人錯誤: *斷言失敗 - [UIKeyboardTaskQueue waitUntilAllTasksAreFinished],/ SourceCache / UIKit / UIKit-2903.2 / Keyey / UIKeyboardTaskQueue.m:368
一點背景,我保存了一些數據后解除了視圖控制器。 數據每次都成功保存。 另外,我最近更改了日期保存代碼,以便在這個方法的主線程上運行,因為我在后台保存了一些問題。
任何想法為什么會這樣?
提前致謝。
當我在一個不是主線程的線程(來自網絡請求中的回調)上調用-presentViewController:animated:completion:
時,我收到了這個錯誤。 解決方案是將您的調用調度呈現並解除對主線程的視圖控制器:
dispatch_async(dispatch_get_main_queue(), ^{
//Code that presents or dismisses a view controller here
});
調用攝像機視圖時遇到了同樣的問題
針對同一問題的Swift語法:
dispatch_async(dispatch_get_main_queue(), {
//Code that presents or dismisses a view controller here
self.presentViewController(imagePicker, animated: true, completion: nil)
})
確保從顯示視圖中關閉視圖控制器。
當需要關閉呈現的視圖控制器時,首選方法是讓呈現視圖控制器關閉它。 換句話說,只要有可能,呈現視圖控制器的同一視圖控制器也應負責解除它。
In the target view controller define delegate and protocol:
@class TargetVC;
@protocol TargetVCDelegate <NSObject>
-(void)dismissTargetVC:(TargetVC*)vc;
@end
@interface TargetVC : UIViewController
@property (nonatomic, weak) id<TargetVCDelegate> delegate;
@end
when you done the job at the target view controller call the delegate:
if( [self.delegate respondsToSelector:@selector(dismissTargetVC:)])
{
[self.delegate dismissTargetVC:self];
}
The implementation of the delegate protocol should be:
-(void)dismissTargetVC:(TargetVC*)vc
{
[vc dismissViewControllerAnimated:YES completion:nil];
// you can get relevant data from vc as you still hold reference to it in this block
// your code ...
}
您必須確保不僅在主線程上調用present / dissmissViewController,而且還必須確保從同一父viewController調用present / dismissViewController。
例如,有兩個navigationController子項。 第一個子視圖控制器為某個作業呈現第二個子節點,該作業將通過委托(接口)返回。 作業完成后,第二個孩子解散自己並調用委托(接口)函數,必須呈現另一個viweController(例如customPopup) - >這會引發錯誤,因為第二個子視圖控制器在彈出窗口調用時不會被解除,當解雇彈出窗口時,已經解散了。
所以在這種情況下:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(400 * NSEC_PER_MSEC)), dispatch_get_main_queue(), { () -> Void in
if let fs = self.scenarios[indexPath.item]{
fs.loadScenario()
sDelegate.onSelectedScenario(fs)
}
})
會做。
如果有人有Assertion Failure問題,那么這里是Swift 3的解決方案:
OperationQueue.main.addOperation{
<your segue or function call>
}
測試時間: Xcode 8.3.2和Swift 3.1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.