UIAlertController Segue to Different Page (Swift)

12,665

Solution 1

You could create a segue in your storyboard, control dragging from the yellow view controller at the top of a scene to a new view controller. Then give that segue an identifier in the inspector. You can call that from the handler in your code.

alert.addAction(UIAlertAction(title:"OK", style: .Default, handler:  { action in self.performSegueWithIdentifier("mySegueIdentifier", sender: self) }

Solution 2

Solution in Swift 3

let alertController = UIAlertController(title: "Email Verification Required", message: "You will receive an email shortly to verify your account this must be done before you can sign in", preferredStyle: .alert)
let backToSignIn = UIAlertAction(title: "OK", style: .cancel, handler: { action in self.performSegue(withIdentifier: "backToSignIn", sender: self)})
alertController.addAction(backToSignIn)
self.present(alertController, animated: true, completion: nil)

Where backToSignIn is the segue name.

Share:
12,665

Related videos on Youtube

Coder
Author by

Coder

Updated on June 04, 2022

Comments

  • Coder
    Coder almost 2 years

    I was able to write a UIAlertController for my button, and it work fine, but I don't know how to segue to a different page once I press the YES or OK button. At the moment, the YES or OK button goes nowhere.

    @IBAction func SubmitButton(sender: AnyObject) {
    
    
            if a > 28.87 {
    
    
                var alert = UIAlertController(title: "H4 with Guide Pins", message: "Can it be removed?", preferredStyle: UIAlertControllerStyle.Alert)
    
                alert.addAction(UIAlertAction(title: "YES", style: UIAlertActionStyle.Default, handler: nil))
    
                alert.addAction(UIAlertAction(title: "NO", style: UIAlertActionStyle.Default, handler: nil))
    
                self.presentViewController(alert, animated: true, completion: nil)
    
            } 
    
            else if data2.text == "" {
    
    
                var alert = UIAlertController(title: "Dimension", message: "Missing Data Input.", preferredStyle: UIAlertControllerStyle.Alert)
    
                alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
    
                self.presentViewController(alert, animated: true, completion: nil)
    
            }
    
            else {
    
    
                var alert = UIAlertController(title: "H4 with Guide Pins", message: "Enter Swallow Dimension.", preferredStyle: UIAlertControllerStyle.Alert)
    
                alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
    
                self.presentViewController(alert, animated: true, completion: nil)
    
                self.view.endEditing(true)
    
            }
    
        }
    
  • Coder
    Coder about 9 years
    I got it to segue, but how do change the ui image when you segue to the other ViewController?
  • Steve Schwedt
    Steve Schwedt about 9 years
    You could call prepareForSegue and use segue.destinationViewController to set an image property in your next View Controller.
  • Coder
    Coder about 9 years
    Ok..Thanks. I will into that.