Swift 2 - 如何在 AlertController 出现时切换到另一个 ViewController?

Swift 2 - How do I switch to another ViewController when the AlertController appears?

单击 AlertController 上的 'OK' 后,我试图切换到另一个 ViewController。当前,当我单击 'OK' 时,它停留在当前 ViewController。如何才能做到这一点?谢谢。

此代码显示在 ViewController1 中,当 AlertController 出现时,我希望在单击 'OK' 后更改为 ViewController4。

代码如下:

  @IBAction func submitTapped(sender: AnyObject) {
    print("Validating...")
    validator.validate(self)
    Button1.hidden = false
}

// MARK: ValidationDelegate Methods

func validationSuccessful() {
    print("Validation Success!")
    let alert = UIAlertController(title: "Success", message: "You are validated!", preferredStyle: UIAlertControllerStyle.Alert)
    let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: {action in ViewController4))
        alert.addAction(defaultAction) - Error Code // Variable used within its own initial value
        self.presentViewController(alert, animated: true, completion: nil)

        } - Error Code //Expected ‘,’ separator

您需要向 OK 操作添加处理程序:

let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: {action in
    performSegueWithIdentifier("controller4SequeIdentifier", sender: nil);
})
alert.addAction(defaultAction)
self.presentViewController(alert, animated: true, completion: nil)

在处理程序中,您可以切换到另一个控制器。请注意,这将在用户按下 OK 按钮后发生。您还需要在故事板中向 ViewController4 添加一个序列,并为其提供一个标识符以匹配传递给 performSegueWithIdentifier.

的标识符