假设一个新的iOS项目,只需使用导航控制器(正确连接为入口点)和覆盖的viewDidAppear(),其中包含以下三行代码:
self.presentViewController(UIViewController(), animated: true, completion: nil)
self.dismissViewControllerAnimated(true, completion: {})
self.presentViewController(UIViewController(), animated: true, completion: nil)
执行时,该代码将发出警告"在演示文稿正在进行时尝试呈现UIViewController!"当试图呈现第二个控制器时。
问题:在调用另一个控制器之前,为了正确解除控制器,我到底错过了什么?
答案 0 :(得分:0)
您需要在初始presentViewController调用上添加某种延迟,如下所示:
override func viewDidAppear(animated: Bool) {
presentViewController(UIViewController(), animated: true) { () -> Void in
self.delay(0.1, closure: { () -> () in
self.dismissViewControllerAnimated(true, completion: nil)
})
}
}
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
似乎在动画真正完成之前调用了完成块。