显示警报时出错
Error While Showing Alert
我正在创建一个注册屏幕并在后端使用 firebase sdk。我已经在我的注册表单中添加了一些 if Conditions,每当表单中的文本字段留空并按下注册按钮时,我都会显示一个带有标题、消息和确定按钮的 UIAlertView
。但是我在 运行 应用程序时看到一个 Thread 异常,它说
Application tried to present modal view controller on itself. Presenting controller is < UIAlertController: 0x7fb750849200>.
并显示堆栈,在错误堆栈的末尾显示
libc++abi.dylib: terminating with uncaught exception of type NSException
我的警报功能代码是
func ShowAlert(Title:String,Message:String){
let alert = UIAlertController(title: Title, message: Message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title:"OK", style: UIAlertActionStyle.default, handler: { (action) in
alert.dismiss(animated: true)
}))
alert.present(alert,animated: true)
}
我感觉是一些主线程问题,但我不知道如何解决。
当前的视图控制器应该显示警报,所以...
present(alert,animated: true)
而不是
alert.present(alert,animated: true)
更新
从您的评论来看,您似乎正试图在后台线程上显示警报。 除了上述,您应该始终在主线程上显示警报(或执行任何UI 操作)…
DispatchQueue.main.async {
showAlert(title: "My title", message: "My Message")
}
旁注
Swift中的所有变量、参数和函数名称应以小写字母开头,e.e.g
func showAlert(title: String, message: String)
而不是
func ShowAlert(Title:String,Message:String)
我正在创建一个注册屏幕并在后端使用 firebase sdk。我已经在我的注册表单中添加了一些 if Conditions,每当表单中的文本字段留空并按下注册按钮时,我都会显示一个带有标题、消息和确定按钮的 UIAlertView
。但是我在 运行 应用程序时看到一个 Thread 异常,它说
Application tried to present modal view controller on itself. Presenting controller is < UIAlertController: 0x7fb750849200>.
并显示堆栈,在错误堆栈的末尾显示
libc++abi.dylib: terminating with uncaught exception of type NSException
我的警报功能代码是
func ShowAlert(Title:String,Message:String){
let alert = UIAlertController(title: Title, message: Message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title:"OK", style: UIAlertActionStyle.default, handler: { (action) in
alert.dismiss(animated: true)
}))
alert.present(alert,animated: true)
}
我感觉是一些主线程问题,但我不知道如何解决。
当前的视图控制器应该显示警报,所以...
present(alert,animated: true)
而不是
alert.present(alert,animated: true)
更新
从您的评论来看,您似乎正试图在后台线程上显示警报。 除了上述,您应该始终在主线程上显示警报(或执行任何UI 操作)…
DispatchQueue.main.async {
showAlert(title: "My title", message: "My Message")
}
旁注
Swift中的所有变量、参数和函数名称应以小写字母开头,e.e.g
func showAlert(title: String, message: String)
而不是
func ShowAlert(Title:String,Message:String)