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

时间:2015-07-21 05:30:39

标签: swift alert uialertcontroller uialertaction

我正在尝试向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 个答案:

答案 0 :(得分:1)

  1. alert代替alertController
  2. alert.addAction应该在okbutton行动之外。
  3. 使用以下代码更改您的代码:

    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)")
            }