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

Rub*_*444 1 swift uialertcontroller

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

我希望用户实际阅读控制器内的消息,我认为这会很烦人,实际上阻止他们盲目点击“确定”

如果可能的话,我将如何开始这样做?

谢谢

Muh*_*han 5

当我们试图修改UIAlertAction. 不过对我来说效果很好。或者,您可以创建类似于 UIAlertController 的自定义视图控制器:

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")
            }
        }
Run Code Online (Sandbox Code Playgroud)

这是它的样子:

在此处输入图片说明