UIAlertController Segue to Different Page(Swift)

时间:2015-02-18 18:49:33

标签: xcode swift segue uialertcontroller

我能够为我的按钮编写一个UIAlertController,它工作正常,但是一旦按下YES或OK按钮,我不知道如何切换到不同的页面。目前,YES或OK按钮无处可去。

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

        }

    }

2 个答案:

答案 0 :(得分:14)

您可以在故事板中创建一个segue,控制从场景顶部的黄色视图控制器拖动到新的视图控制器。然后在检查器中为该segue指定一个标识符。您可以从代码中的处理程序中调用它。

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

答案 1 :(得分:2)

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)

其中backToSignIn是segue名称。

相关问题