Search code examples
iosswiftsegueuialertcontroller

Alert Blocking Segue-iOS


I want to ask the user for permission (for which I am using UIAlert) before performing the segue. Once they have answered the question in the alert, I want to segue to the next View Controller, irrespective of their answer.

The code looks something like this:

showAlert()        //Method showing the alert

performSegue(withIdentifier : "secondVC", sender : self)

The problem I am facing here is that the app is showing me the alert but not performing the segue.


Solution

  • Add a completion handler to your alert's dismiss button like this

    let alert = UIAlertController(title: "Alert", message: "Content Here", preferredStyle: .alert)
    
    // add textfield or whatever you need
    
    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
        self.performSegue(withIdentifier: "secondVC", sender: self)
    }))
    present(alert, animated: true)
    

    The completion handler will be called when the user presses the "OK" button on the alert.