Ios 在Swift 4中显示和解除UIAlertController
我为UIAlertViewController声明了一个全局变量,以便能够在类中的不同方法中显示和消除它。 我显示了两种警报:第一种是带有按钮的警报,当遇到错误时将显示该按钮,或者显示信息消息。第二个是没有按钮的警报,它将显示为进度消息 以下是示例代码:Ios 在Swift 4中显示和解除UIAlertController,ios,swift4,uialertcontroller,Ios,Swift4,Uialertcontroller,我为UIAlertViewController声明了一个全局变量,以便能够在类中的不同方法中显示和消除它。 我显示了两种警报:第一种是带有按钮的警报,当遇到错误时将显示该按钮,或者显示信息消息。第二个是没有按钮的警报,它将显示为进度消息 以下是示例代码: private var alert: UIAlertController? // global declaration private func showProgressMessage(sender viewController: UIVie
private var alert: UIAlertController? // global declaration
private func showProgressMessage(sender viewController: UIViewController, message alertMessage: String)
{
DispatchQueue.main.async
{
self.alert= UIAlertController(title: "", message: alertMessage, preferredStyle: .alert)
viewController.present(self.alert!, animated: true, completion: nil)
}
}
private func showAlertMessage(sender viewController: UIViewController, title alertTitle: String, message alertMessage: String)
{
DispatchQueue.main.async
{
self.alert= UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
self.alert!.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
viewController.present(self.alert!, animated: true, completion: nil)
}
}
private func method1()
{
DispatchQueue.global().async
{
// some code here
self.showProgressMessage(sender: self, message: "Processing...")
// some code here
}
}
private func method2()
{
// some code here
self.alert!.dismiss(animated: false)
{
self.showAlertMessage(sender: self, message: "Done")
}
self.displayOtherViewController()
}
private func displayOtherViewController()
{
self.alert?.dismiss(animated: false)
{
if let viewController = self.storyboard?.instantiateViewController(withIdentifier: "Sample")
{
let view = viewController as! SampleViewController
view .modalTransitionStyle = .crossDissolve
self.present(view , animated: true, completion: nil)
}
}
}
在方法2中,再次显示警报需要几秒钟的时间,与视图控制器相同。
在Swift 4中显示和禁用UIAlertController的正确方式是什么?您的代码似乎是从后台线程启动的。 甚至必须在主线程上调用design 试试这个:
private func method2() {
DispatchQueue.main.async {
self.alert!.dismiss(animated: false) {
self.showAlertMessage(sender: self, message: "Done")
}
self.displayOtherViewController()
}
}
动画需要几秒钟?只需将动画属性设置为
false
。如果您添加gif以显示正在发生的事情和您想要的内容,我们将能够更好地理解。对不起,应该是毫秒。有时,警报根本不显示。我已经将动画更改为false,但得到了相同的结果。