Perform segue after UIAlertController is dismissed

孤人 提交于 2019-12-02 01:52:39

问题


I've had similar problems in the past with UIAlertController in the sense that there's always a lag on the UI thread after the UIAlertController is dismissed.
My problem right now is that I want to perform a segue if the user clicks on the "Okay" UIAlertAction and nothing happens if "Cancel" UIAlertAction is pressed.
This is my code:

// 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)

What is currently happening is when I tap "Okay" the segue is performing, but I can only see the last moments of the transition (i.e the animation starts while the UIAlertController is being dismissed).
How do I make the segue start once the UIAlertController is dismissed?

Note - I prefer not solving this in a hacky method like performing the segue after a fixed delay if there's another way.

Thanks!


回答1:


The problem is in this code:

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

The handler: is not a completion handler. It runs before the alert is (automatically) dismissed. Thus, you are starting the segue while the alert is still present.

If you prefer not to use delay (though I see nothing wrong with that approach), what I would try is this:

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


来源:https://stackoverflow.com/questions/40367565/perform-segue-after-uialertcontroller-is-dismissed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!