Swift:函数中的 UIAlert - 使用未解析的标识符 'present'
Swift: UIAlert in function - Use of unresolved identifier 'present'
我正在尝试限制代码的显示,所以我只想调用包含两个字符串的函数来使用 1 行而不是 5 行更快地创建 uialert/
我遇到的错误
Use of unresolved identifier 'present'
在第
行
present(alert, animated: true, completion: nil)
// 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."
)
我正在尝试限制代码的显示,所以我只想调用包含两个字符串的函数来使用 1 行而不是 5 行更快地创建 uialert/
我遇到的错误
Use of unresolved identifier 'present'
在第
行present(alert, animated: true, completion: nil)
// 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."
)