关闭UIAlertController后执行segue

uti*_*nia 2 segue swift uialertcontroller

在过去,我曾经遇到过类似的问题,即UIAlertControllerUIAlertController取消后,UI线程上总是存在滞后。
我现在的问题是,如果用户单击“确定”,我想执行顺序检查,如果按下UIAlertAction“取消”,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)
Run Code Online (Sandbox Code Playgroud)

当前正在发生的是,当我点击“确定”时,segue正在执行,但是我只能看到过渡的最后时刻(即,动画在UIAlertController被关闭时开始播放)。
一旦UIAlertController解散,我如何使segue开始?

注意-如果有其他方法,我宁愿不要以骇人的方式解决此问题,例如在固定的延迟后执行segue。

谢谢!

mat*_*att 5

问题出在以下代码中:

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

handler:不是一个完成处理程序。它警报自动(自动)消除之前运行。因此,您在警报仍然存在的同时开始segue。

如果您不想使用delay(尽管我看不到这种方法有什么不对),我将尝试这样做:

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

  • 公平地说,我说的是一种骇客的方法。而且我认为,如果有一种方法在某件事发生后调用动作,那么在经过一段硬编码的时间后执行某件事并不是一个好习惯。无论如何感谢您的帮助 (2认同)
  • 我不明白为什么一种方法比另一种方法或多或少地“令人讨厌”。无论哪种方式,您都在解决API的(奇怪)限制。每当您必须执行此操作时,这都是黑客行为(也是API中的缺陷)。但这意味着您应该向Apple提出错误,而不是告诉我要给出什么样的答案。 (2认同)