gpt4 book ai didi

swift - NSTimer 值不更新 UILabel?在 SWIFT

转载 作者:行者123 更新时间:2023-11-28 07:08:51 25 4
gpt4 key购买 nike

我正在尝试使用 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 值不更新 UILabel?在 SWIFT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29329727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com