Shekar Shekar - 4 years ago 96
iOS Question

Alert and segue in same method not working together

I want to segue to my home screen after the user clicks the ":(" (button part of the UIAlert. However, when the user clicks the ":(", the view does not change. When I remove the UIAlert, and only have the segue, the method works in that it segues to the home screen. How can I do both?

let gameOver = UIAlertController(title: "BOOM", message: "You hit a mine! Game over.", preferredStyle: UIAlertControllerStyle.alert)
gameOver.addAction(UIAlertAction(title: ":(", style: UIAlertActionStyle.default, handler: nil))
self.present(gameOver, animated:true, completion: nil)
self.performSegue(withIdentifier: "homeScreen", sender:self)

Answer Source

For that you need to performSegue with your action handler, currently you are setting it to nil.

let gameOver = UIAlertController(title: "BOOM", message: "You hit a mine! Game over.", preferredStyle: .alert)
gameOver.addAction(UIAlertAction(title: ":(", style: .default) { action in 
     self.performSegue(withIdentifier: "homeScreen", sender:self)
})
self.present(gameOver, animated:true)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download