簡體   English   中英

Swift:函數中的UIAlert-使用未解析的標識符“ present”

[英]Swift: UIAlert in function - Use of unresolved identifier 'present'

我試圖限制代碼的顯示,所以我只想調用包含兩個字符串的函數以1行而不是5 /更快地創建uialert

我得到的錯誤

使用未解決的標識符“存在”

在行

目前(警告,動畫:正確,完成:無)

// 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 ,則應該可以使用,例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM