在 UIAlertController 中更新倒数计时器时出现问题

Problems updating countdown timer in UIAlertController

我正在尝试更新 UIAlertController 的消息部分中的倒数计时器。我在这里尝试了很多建议的答案,但没有任何乐趣。 我有一个简单的 ViewController 来尝试我想要实现的目标。

class ViewController: UIViewController {

var counter:Int = 5
var timer: NSTimer?
var label = ""
var message = "in "

override func viewDidLoad() {
    super.viewDidLoad()
}

override func viewDidAppear(animated: Bool) {
    showAlert()
}

func showAlert(){
    let alertController = UIAlertController(title: "Try again ", message: countDownString(), preferredStyle: .Alert)

    presentViewController(alertController, animated: true){
        self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(self.decrease), userInfo: nil, repeats: true)
    }
}

func decrease()
{
    var minutes: Int
    var seconds: Int
    if(counter > 0) {
        self.counter -= 1
        print(counter)  // Correct value in console
        minutes = (counter % 3600) / 60
        seconds = (counter % 3600) % 60
        label = String(format: "%02d:%02d", minutes, seconds)
        print(label)  // Correct value in console
    }
    else{
        dismissViewControllerAnimated(true, completion: nil)
        timer!.invalidate()
    }
}

func alertMessage() -> String {
    print(message+"\(self.label)")
    return(message+"\(self.label)")
}

func countDownString() -> String {
    print("\(counter) seconds")
    return "\(counter) seconds"
}

}

Counter 和 Label 都在控制台中显示了正确的值,但我无法让它们在 UIAlertController 中显示这些值。 屏幕截图显示了我在视图控制器中获得的输出。

我哪里错了?

我正在使用 Xcode 7.3 和 Swift 2.2

当您在下面的代码中实例化您的 UIAlertController 时,来自 countDownString() 的当前值被复制为消息并且无法以这种方式更新

let alertController = UIAlertController(title: "Try again ", message: countDownString(), preferredStyle: .Alert)

您应该将 UIAlertController 存储为实例 属性,如下所示:

var alertController: UIAlertController!

showAlert() 的代码更改为:

func showAlert(){
    alertController = UIAlertController(title: "Try again ", message: countDownString(), preferredStyle: .Alert)
    presentViewController(alertController, animated: true){
        self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(self.decrease), userInfo: nil, repeats: true)
    }
}

并直接在 decrease() 函数中更新 alertController 消息:

func decrease()
{
    var minutes: Int
    var seconds: Int
    if(counter > 0) {
        self.counter -= 1
        print(counter)  // Correct value in console
        minutes = (counter % 3600) / 60
        seconds = (counter % 3600) % 60
        alertController.message = String(format: "%02d:%02d", minutes, seconds)
        print("\(minutes):\(seconds)")  // Correct value in console
    }
    else{
        dismissViewControllerAnimated(true, completion: nil)
        timer!.invalidate()
    }
}
 var timer:Timer?
    var timeLeft = 300

    timeLeft = 300
        timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(onTimerFires), userInfo: nil, repeats: true)

  @objc func onTimerFires() {
       
        var minutes: Int
        var seconds: Int

        if timeLeft == 0 {
            timer?.invalidate()
        }
        timeLeft = timeLeft - 1
       // hours = timeLeft / 3600
        minutes = (timeLeft % 3600) / 60
        seconds = (timeLeft % 3600) % 60
        timeLabel.text = String(format: "%02d:%02d", minutes, seconds)
    }