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

9lowa7mx  于 2022-12-10  发布在  Swift
关注(0)|答案(3)|浏览(171)

是否可以创建一个UIAlertController,其中包含一个按钮,该按钮最初被禁用,然后依次为5、4、3..2..1,最后被启用。
我希望用户真正阅读控制器内的消息,我认为这将是恼人的,足以实际上阻止他们盲目点击确定
如果可能的话,我该如何开始做这件事呢?
谢谢

yqhsw0fo

yqhsw0fo1#

这有点麻烦,因为我们正试图修改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")
            }
        }

它看起来是这样的:

4si2a6ki

4si2a6ki2#

是,您可以通过以下方式在特定间隔后启用警报操作:

func showAlert() {
    let alertview = UIAlertController(title: "title", message: "msg", preferredStyle: .Alert)
    let action = UIAlertAction(title: "OK", style: .Default, handler: nil)
    alertview.addAction(action)
    self.presentViewController(alertview, animated: true) { () -> Void in
        action.enabled = false
        NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "enableBtn:", userInfo: action, repeats: false)

    }
}

func enableBtn(t: NSTimer){
    let info = t.userInfo as! UIAlertAction
    info.enabled = true
}

希望对你有帮助:)

fslejnso

fslejnso3#

这是Accepted Answermy 变体
我发现我必须使UIAlertController中提供给用户的每个选项的计时器无效,否则计时器将继续在后台运行,并导致一些其他问题。
我自己的实现有点复杂,有更多的选择,但要点如下。也更新到Xcode 14

var timer: Timer?
  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.isEnabled = false
    
    alertController.addAction(okAction)
    
    alertController.addAction(UIAlertAction(title: "No", style: .cancel, handler: {( action: UIAlertAction ) in
      print("No")
      self.invalidateAlertControllerCountDownTimer()
    }))

    alertController.addAction(UIAlertAction(title: "Maybe", style: .cancel, handler: {( action: UIAlertAction ) in
      print("Maybe")
      self.invalidateAlertControllerCountDownTimer()
    }))

    
    present(alertController, animated: true) {
      self.timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.alertControllerCountDownTimer), userInfo: nil, repeats: true)
    }
  }
  
  @objc func alertControllerCountDownTimer() {
    timerCount -= 1
    
    let alertController = presentedViewController as! UIAlertController
    
    if let alertController = alertController {
      let okAction = alertController.actions.first
      
      if timerCount == 0 {
        invalidateAlertControllerCountDownTimer()
        okAction?.setValue("Ok", forKey: "title")
        okAction?.isEnabled = true
      } else {
        okAction?.setValue("Ok \(timerCount)", forKey: "title")
      }
    }
  }
  
  func invalidateAlertControllerCountDownTimer() {
    timer?.invalidate()
    timer = nil
    timerCount = 5
  }

相关问题