警报完成未完全正常工作

alert completion not fully working

我有一个警报,在警报显示后,我想展示一个不同的取景器。 doSomething() 函数被触发, "TEST" 被打印,但新的取景器没有出现。我错过了什么?

警报

let alert = UIAlertController(title: "Sorry", message: "Booked out.",
preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style:
UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: self.doSomething)

内容函数 doSomething()

print("TEST")
let details = storyboard?.instantiateViewController(withIdentifier: "ViewLimo2")
details?.transitioningDelegate = slideAnimatorRight
present(details!, animated: true, completion: nil)

视图控制器上的完成块在关闭视图控制器时不会触发。它在视图控制器完成呈现时触发(例如,它已完成 viewDidAppear)。

老实说,我预计这会崩溃,因为您在警报仍在显示时尝试显示。

在任何情况下,您都需要等到 UIAlertController 解雇后才能尝试呈现下一个 View Controller。

您可以在 OK 操作的处理程序中执行此操作:

    let alert = UIAlertController(title: "Sorry", message: "Booked out.",
                                  preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "OK", style:
        UIAlertActionStyle.default, handler: doSomething))
    self.present(alert, animated: true, completion: nil)

...

func doSomething(action:UIAlertAction) {
    /// present the next VC here
}