我正在创建一个注册屏幕,并在后端使用firebase sdk。我在我的注册表单中添加了一些if条件,如果表单中的文本字段为空并按下了注册按钮,我将显示一个带有标题、消息和确定按钮的UIAlertView
。但我在运行应用程序时看到线程异常
应用程序试图在其自身上呈现模式视图控制器。呈现控制器是< UIAlertController: 0x7fb750849200>。
并显示堆栈,在错误堆栈的末尾显示
libc++abi.dylib:使用类型NSException的未捕获异常终止
我的alertfunction函数的代码是
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)
}
我的感觉是,这是一些主线问题,但我不知道如何解决。
发布于 2018-08-05 20:19:25
当前视图控制器应该显示警报,因此只显示…
present(alert,animated: true)
而不是
alert.present(alert,animated: true)
更新
从您的评论中,听起来像是您试图在后台线程上显示警报。除了以上内容之外,您应该始终在主线程…上显示警报(或执行任何UI操作
DispatchQueue.main.async {
showAlert(title: "My title", message: "My Message")
}
附注
Swift中的所有变量、参数和函数名都应以小写字母开头,例如
func showAlert(title: String, message: String)
而不是
func ShowAlert(Title:String,Message:String)
https://stackoverflow.com/questions/51694223
复制相似问题