gpt4 book ai didi

swift - UIAlertController 中的倒计时

转载 作者:行者123 更新时间:2023-11-28 12:51:14 25 4
gpt4 key购买 nike

我想在我的警报 Controller 中倒计时 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)
}

最佳答案

在你设置延迟时间来关闭你的 View Controller 之后,你可以设置一个定时器来在一秒后调用一个函数,比如:

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

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

关于swift - UIAlertController 中的倒计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36450931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com