Countdown in UIAlertController

此生再无相见时 提交于 2019-12-13 07:17:55

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/36450931/countdown-in-uialertcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!