xcode UIAlertController 转至不同页面 (Swift)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28591514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:40:42  来源:igfitidea点击:

UIAlertController Segue to Different Page (Swift)

xcodeswiftsegueuialertcontroller

提问by Coder

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.

我能够为我的按钮编写一个 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)

        }

    }

回答by Steve S

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.

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

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

回答by DoesData

Solution in Swift 3

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 backToSignInis the segue name.

backToSignInsegue 名称在哪里。