在UIAlertController中更新倒计时器的问题

时间:2016-11-23 22:07:15

标签: ios swift 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中显示值。 屏幕截图显示了我在视图控制器中获得的输出。

我哪里错了?

changes between 2.1.x to 2.2.x alertMessage screenshot

我正在使用Xcode 7.3和Swift 2.2

2 个答案:

答案 0 :(得分:1)

当您在下面的代码中实例化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)
    }
}

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

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

答案 1 :(得分:0)

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