本文介绍了UIAlertController中的倒计时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的警报控制器中倒数3秒。不确定如何执行此操作。我可以在3秒内使警报消失,只是缺少倒计时。
I would like to countdown 3 seconds in my alert controller. Not sure how to do this. I can get the alert to disappear in 3 seconds, just the countdown is missing.
//simple alert dialog
let alertController = UIAlertController(title: "Workflow successful", message: "Modified reminder's priority or list.", preferredStyle: .Alert)
var secs = 3
let defaultAction = UIAlertAction(title: "Dismiss in " + secs, style: .Default, handler: nil)
alertController.addAction(defaultAction)
dispatch_async(dispatch_get_main_queue()) {
//self.presentViewController(alertController, animated: true, completion: nil)
self.showViewController(alertController, sender: self)
}
let delayTime = dispatch_time(DISPATCH_TIME_NOW,
Int64(secs * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
alertController.dismissViewControllerAnimated(true, completion: nil)
}
推荐答案
设置del后没时间关闭视图控制器时,您可以设置一个计时器以在第二秒后调用函数:
After you set the delay time to dismiss your view controller, you can set a timer to call a function after a second like:
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(self.decrease), userInfo: nil, repeats: true)
func decrease()
{
self.secs-=1
self.defaultAction.setValue("Dismiss in \(self.secs)", forKey: "title")
}
它将用您的新时间更新标题。
And it will update the title with your new time.
这篇关于UIAlertController中的倒计时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!