Swift:UIAlert in function - 使用未解析的标识符'present'
我试图限制代码的显示,所以我只想调用包含两个字符串的函数来创建一个更快的快速1行而不是5/Swift:UIAlert in function - 使用未解析的标识符'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)
}
在评论,你解释说,这是一个独立的功能。如果你把它扩展到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."
)
谢谢!以及如果我也想在系统类中调用它,那么在编写showAlert(“”,“”)时会出现错误“使用未解析标识符showAlert'' – BroSimple
反正在ViewController之外调用它? – BroSimple
由于'present'是'UIViewController'中的一个方法,所以你需要扩展*那个*。正如所写的,只要* system class *在你调用的时候是'UIViewController'的子类,那你就很好。我不确定你到底在做什么,但是它需要一个视图控制器来展示另一个视图控制器 - 我相信你知道它是基本的MVC。第二个评论要跟着...... – dfd
最常见的原因(我相信)是因为这个代码/函数不在'UIViewController'里面。是这样吗? – dfd
什么是“自我”?它需要是一个'UIViewController'。 – rmaddy
从哪里调用此片段? –