本文介绍了Swift:函数中的UIAlert-使用未解析的标识符"present"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to limit the show of code so I just want to call function containing two strings to create a uialert faster with 1 line instead of 5/

我遇到的错误

在线

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

推荐答案

在注释中,您解释说这是一个独立的函数.如果您将其扩展为 UIViewController ,例如:

In the comments, you explained that this is a stand-alone function. It should work if you make it an extension to UIViewController, for instance:

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

这篇关于Swift:函数中的UIAlert-使用未解析的标识符"present"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 11:01