在 UIAlertController 被关闭后执行 segue

Perform segue after UIAlertController is dismissed

我过去在使用 UIAlertController 时遇到过类似的问题,即在 UIAlertController 被关闭后 UI 线程总是有延迟。
我现在的问题是,如果用户单击 "Okay" UIAlertAction 我想执行 segue,如果按下 "Cancel" UIAlertAction 则什么也不会发生。
这是我的代码:

// create the uialertcontroller to display
let alert = UIAlertController(title: "Do you need help?",
                              message: "Would you like to look for a consultant?",
                              preferredStyle: .alert)
// add buttons
let okay = UIAlertAction(title: "Yes, please.", style: .default, handler: {_ in
    self.performSegue(withIdentifier: "segue", sender: nil)
})
let no = UIAlertAction(title: "No, I'm okay.", style: .cancel, handler: nil)
alert.addAction(okay)
alert.addAction(no)
self.present(alert, animated: true, completion: nil)

当前发生的情况是,当我点击 "Okay" 时,segue 正在执行,但我只能看到过渡的最后时刻(即动画在 UIAlertController 被关闭时开始) .
UIAlertController 被关闭后,我该如何开始 segue?

注意 - 如果有其他方法,我不喜欢用像在固定延迟后执行 segue 这样的 hacky 方法来解决这个问题。

谢谢!

问题出在这段代码中:

let okay = UIAlertAction(title: "Yes, please.", style: .default, handler: {_ in
    self.performSegue(withIdentifier: "segue", sender: nil)
})

handler: 不是完成 处理程序。它运行 before 警报被(自动)解除。因此,您在警报仍然存在时开始 segue。

如果您不想使用 delay(尽管我认为这种方法没有任何问题),我会尝试这样做:

let okay = UIAlertAction(title: "Yes, please.", style: .default, handler: {_ in
    CATransaction.setCompletionBlock({
        self.performSegue(withIdentifier: "segue", sender: nil)
    })
})