有没有办法让警报 View 在几秒钟后自动消失,无需用户操作。目前我的代码如下,它要求用户按确定以消失警报对话框。我想显示警报而不需要用户干预,只需让警报在几秒钟内消失。感谢您的任何评论。
我的代码如下:
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)
}
最佳答案
您可以使用dispatch_after
延迟任何事情。例如,这将在 3 秒后关闭警报 View 。
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);
}
您还可以使用@matt's awesome delay function .
关于 swift 代码 : Is there a way to make the alert view disappear automatically,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30362063/