解除视图控制器时出错

在解散视图控制器时出现错误,之前没有看到类似的东西,在互联网上没有太多关于它的信息。

inheritance人的错误: *断言失败 – [UIKeyboardTaskQueue waitUntilAllTask​​sAreFinished],/SourceCache/UIKit/UIKit-2903.2/Keyboard/UIKeyboardTaskQueue.m:368

有点背景,我保存了一些数据后closures了视图控制器。 每次数据保存成功。 另外,我最近更改了date保存代码,以便在这个方法的主线程上运行,因为我在后台保存了一些问题。

任何想法为什么发生这种情况?

提前致谢。

当我调用-presentViewController:animated:completion:在不是主线程(来自networking请求中的callback)的线程上时,我收到了此错误。 解决方法是派遣你的调用来呈现和解雇视图控制器到主线程:

 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) }) 

确保从演示视图closures视图控制器。

https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html

当需要closures一个呈现的视图控制器时,首选的方法是让呈现视图控制器closures它。 换句话说,只要有可能,呈现视图控制器的视图控制器也应该负责解除视图控制器。

 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,还必须确保present / dismissViewController是从同一个父viewController调用的。

例如有两个navigationController子项。 第一个子视图控制器为第二个孩子提供一个将通过委托(界面)返回的作业。 在作业完成后,第二个孩子自行解除并调用委托(接口)函数,而不得不呈现另一个viweController(例如customPopup) – >这是错误的,因为第二个子视图控制器不会被撤销,但在popup窗口被取消时已经消失。

所以在这种情况下:

  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) } }) 

会做。

如果有人有断言失败问题,那么这里是Swift 3的解决scheme:

 OperationQueue.main.addOperation{ <your segue or function call> } 

testing: Xcode 8.3.2和Swift 3.1