Swift:功能中的UIAlert - 使用未解析的标识符' present'

时间:2017-08-17 17:27:30

标签: ios swift uialertview

我试图限制代码的显示,所以我只想调用包含两个字符串的函数来创建一个更快的uialert,而不是5 /

我得到的错误

  

使用未解析的标识符' present'

  

现在(警报,动画:真实,完成:无)

// Controlling Alerts for Errors
func showAlert(titleString: String, messageString: String) {

 // Alert to go to Settings
 let alert = UIAlertController(title: titleString, message: messageString, preferredStyle: .alert)

 alert.addAction(UIAlertAction(title: "Dismiss", style: .default, handler: { _ in
     alert.dismiss(animated: true, completion: nil)
 }))

 self.present(alert, animated: true, completion: nil)
}

1 个答案:

答案 0 :(得分:1)

在评论中,您解释说这是一个独立的功能。如果您将其作为UIViewController的扩展名,它应该有效,例如:

extension UIViewController {
    public func showAlert(_ title:String, _ message:String) {
        let alertVC = UIAlertController(
            title: title,
            message: message,
            preferredStyle: .alert)
        let okAction = UIAlertAction(
            title: "OK",
            style: .cancel,
            handler: { action -> Void in
        })
        alertVC.addAction(okAction)
        present(
            alertVC,
            animated: true,
            completion: nil)
    }

}

并在UIViewController

中调用它
showAlert(
    "Could Not Send Email", 
    "Your device could not send e-mail.  Please check e-mail configuration and try again."
)