user10460877
user10460877

Reputation:

Stop UI Alert Controller going back to previous view

I have a UIAlertView within my app. But for some strange reason if I decide to press the "Back" button on the Alert View. It actually takes me to the previous view controller rather than simply dismissing the alert.

How can I prevent this? I only want to hid the alert if the back button is pressed

Video of the issue: https://youtu.be/fhb-REuQiyg

Here's the function for the alert:

let alertController = UIAlertController.init(title: "Open Map", message: "", preferredStyle: .alert)
alertController.addAction(UIAlertAction.init(title: "Google Maps", style: .default, handler: { (action) in
    self.googleMapsOpen()
}))
alertController.addAction(UIAlertAction.init(title: "Apple Maps", style: .default, handler: { (action) in
    self.appleMapsOpen()
}))
alertController.addAction(UIAlertAction.init(title: "Back", style: .default, handler: { (action) in
    self.dismiss(animated: true, completion: nil)

}))
self.present(alertController, animated: true) {
}

Upvotes: 0

Views: 941

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100513

Remove this

self.dismiss(animated: true, completion: nil)

Here

alertController.addAction(UIAlertAction.init(title: "Back", style: .default, handler: { (action) in
    self.dismiss(animated: true, completion: nil) 
}))

the alert controller is dismissed automatically when you press the action , so your written dismiss will dismiss the current presented vc also

Upvotes: 3

Related Questions