Is there a way to make the alert view disappear automatically.. after some seconds, without user action. Currently I have my code as follow, and it require user to press ok to disappear the alert dialog. I would like to show the alert and not have user intervention, and just have the alert disappear in few seconds. Thanks for any comments.
有什么方法可以使警报视图自动消失几秒钟后,没有用户操作。目前我的代码如下所示,需要用户按ok才能消失警报对话框。我想显示警报,没有用户干预,并让警报在几秒钟内消失。谢谢你的任何评论。
My code as below:
我的代码如下:
func showAlertController (message: String) {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(alertController, animated: true, completion: nil)
}
2 个解决方案
#1
2
You can delay anything with dispatch_after
. For example, this would dismiss the alert view after 3 seconds.
使用dispatch_after可以延迟任何内容。例如,这将在3秒后关闭警报视图。
let delayTime = dispatch_time(DISPATCH_TIME_NOW,
Int64(3 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
presentedViewController.dismissViewControllerAnimated(true, completion: nil);
}
You can also use @matt's awesome delay function.
你也可以使用@matt的延迟函数。
#1
2
You can delay anything with dispatch_after
. For example, this would dismiss the alert view after 3 seconds.
使用dispatch_after可以延迟任何内容。例如,这将在3秒后关闭警报视图。
let delayTime = dispatch_time(DISPATCH_TIME_NOW,
Int64(3 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
presentedViewController.dismissViewControllerAnimated(true, completion: nil);
}
You can also use @matt's awesome delay function.
你也可以使用@matt的延迟函数。