UIAlertController中的倒计时

时间:2016-04-06 12:18:19

标签: swift delay uialertcontroller

我想在警报控制器中倒计时3秒。不知道该怎么做。我可以在3秒内让警报消失,只是倒计时丢失。

//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)
}

1 个答案:

答案 0 :(得分:2)

设置延迟时间以关闭视图控制器后,您可以设置一个计时器以在一秒钟之后调用一个函数,如:

pending changes

它会用你的新时间更新标题。