我正在尝试使用NSTimer.scheduledTimerWithTimeInterval方法更新一个标签,虽然计时器工作正常,但我的标签并没有像它应该的那样更改,有人能告诉我我做错了什么吗?代码:

class Questions: UIViewController, UITextFieldDelegate {

    var timerVal = 10
    var timerLbl :UILabel!
    var timer:NSTimer!

    override func viewDidLoad() {
        super.viewDidLoad()

      startTimer()
    }


    func timerLabel() {
        var timerLbl = UILabel(frame: CGRectMake(250, 20, 50, 50))
        timerLbl.text = "\(10)"
        timerLbl.textColor = UIColor.blackColor()
        timerLbl.font = UIFont(name: Font.FuturaBlack, size: 30)
        timerLbl.textAlignment = NSTextAlignment.Center

        view.addSubview(timerLbl)
    }

    func updateTimer(dt:NSTimer) {
        timerVal--
        if timerVal == 0{
           timerVal = 11
        }else if timerVal < 0{
            timer.invalidate()
        } else{
            println(timerString) //PRINTS CORRECT AS THE TIMER DECREASES BY ONE SECOND
            timerLbl.text = "\(timerVal)" //NOTHING HAPPENS ON THE LABEL
        }
    }

    func startTimer(){
        timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("updateTimer:"), userInfo: nil, repeats: true)
    }

}

最佳答案

创建timerLbl的函数timerLabel永远不会被调用。

关于swift - NSTimer Value是否不更新UILabel?在SWIFT中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29329727/

10-10 17:35