无法向 UIAlertController 添加警报操作 (Swift)

Can't add an Alert Action to UIAlertController (Swift)

我正在尝试向 UI 警报添加确定按钮,但我无法使用 alertController.addAction

添加它

遇到这种情况我该怎么办?

提前致谢!!

if error == nil {
                let alert: UIAlertController = UIAlertController(title: "Account Created", message: "Please confirm your email", preferredStyle: .Alert)

                let okButton = UIAlertAction(title: "Ok", style: .Default) { action -> Void in
                    self.performSegueWithIdentifier("toMain", sender: self)

                    alertController.addAction(okButton)


                self.presentViewController(alert, animated: true, completion: nil)

                }

                } else {

                println("\(error)")
            }
  1. alert 而不是 alertController
  2. alert.addAction 应该在您的 okbutton 操作之外。

更改您的代码:

if error == nil {
    let alert: UIAlertController = UIAlertController(title: "Account Created", message: "Please confirm your email", preferredStyle: .Alert)

    let okButton = UIAlertAction(title: "Ok", style: .Default) { action -> Void in
        self.performSegueWithIdentifier("toMain", sender: self)

    }
    alert.addAction(okButton)

    self.presentViewController(alert, animated: true, completion: nil)
 } else {

            println("\(error)")
        }