我正在尝试使用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)
}
}
答案 0 :(得分:1)
永远不会调用创建timerLbl的函数timerLabel。