uti*_*nia 2 segue swift uialertcontroller
在过去,我曾经遇到过类似的问题,即UIAlertController在UIAlertController取消后,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。
谢谢!
问题出在以下代码中:
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)
| 归档时间: |
|
| 查看次数: |
2370 次 |
| 最近记录: |