假设一个新的 iOS 项目,只有一个导航 Controller (正确连接为入口点)和一个包含以下三行代码的重写 viewDidAppear():
self.presentViewController(UIViewController(), animated: true, completion: nil)
self.dismissViewControllerAnimated(true, completion: {})
self.presentViewController(UIViewController(), animated: true, completion: nil)
执行时,该代码将发出警告“在演示过程中尝试显示 UIViewController!”尝试展示第二个 Controller 时。
问题:为了在调用另一个 Controller 之前正确关闭 Controller ,我究竟缺少什么?
最佳答案
您需要在最初的 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)
}
似乎在动画真正完成之前调用了完成 block 。
关于ios - 尝试在演示过程中显示 UIViewController!-警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32460224/