在 UIAlertController 中更新倒数计时器的问题 [英] Problems updating countdown timer in UIAlertController

查看:16
本文介绍了在 UIAlertController 中更新倒数计时器的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to update a countdown timer in the message part of a UIAlertController. I've tried lots of answers suggested on here but with no joy. I have a simple ViewController to try out what I'm trying to achieve.

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

Counter and Label both show the correct values in the console but I'm unable to get them to display the values in the UIAlertController. screenshots show the output i'm getting in the view controller.

我哪里出错了?

我使用的是 Xcode 7.3 和 Swift 2.2

I'm using Xcode 7.3 and Swift 2.2

推荐答案

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

When you instantiate your UIAlertController in the code below, current value from countDownString() is copied as message and cannot be updated this way

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

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

You should store your UIAlertController as instance property, something like this:

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 消息:

and update alertController message directly in your decrease() function:

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

这篇关于在 UIAlertController 中更新倒数计时器的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆