【发布时间】:2017-08-17 17:27:29
【问题描述】:
我试图限制代码的显示,所以我只想调用包含两个字符串的函数,以用 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内。是这样吗? -
什么是
self?它必须是UIViewController。 -
从哪里调用这个 sn-p ?
-
@dfd 正确,它只是一个我用来组织代码的 swift 文件,例如我称之为 System.showAlert("Title", "The Message")
-
否则你可以传递你想要展示 UIViewController 的视图控制器
标签: ios swift uialertview