gpt4 book ai didi

swift - UIAlertController 中按钮内显示的倒数计时器然后启用按钮 swift

转载 作者:行者123 更新时间:2023-11-28 10:18:55 25 4
gpt4 key购买 nike

是否可以创建一个 UIAlertController,它有一个按钮,该按钮最初是禁用的,然后是 5,然后是 4,然后是 3..2..1,然后是启用的。

我希望用户真正阅读 Controller 内的消息,我认为这会很烦人,实际上会阻止他们无意识地点击“确定”

如果可能的话,我将如何着手做这件事?

谢谢

最佳答案

当我们试图修改 UIAlertAction 的只读属性时,它有点老套。但是它对我来说很好用。或者,您可以创建看起来像 UIAlertController 的自定义 View Controller :

var timer: NSTimer?
var timerCount: Int = 5
func showAlertView() {
let alertController = UIAlertController(title: "Title", message: "Message of alert", preferredStyle: .Alert)

let okAction = UIAlertAction(title: "Ok", style: .Default, handler: nil)
okAction.enabled = false

alertController.addAction(okAction)
alertController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
presentViewController(alertController, animated: true) {
self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(self.countDownTimer), userInfo: nil, repeats: true)
}
}

func countDownTimer() {
timerCount -= 1

let alertController = presentedViewController as! UIAlertController
let okAction = alertController.actions.first

if timerCount == 0 {
timer?.invalidate()
timer = nil

okAction?.setValue("Ok", forKey: "title")
okAction?.enabled = true
} else {
okAction?.setValue("Ok \(timerCount)", forKey: "title")
}
}

它是这样的:

enter image description here

关于swift - UIAlertController 中按钮内显示的倒数计时器然后启用按钮 swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37338058/

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