Reputation: 2237
I am trying to update a label using the NSTimer.scheduledTimerWithTimeInterval
method although the timer works fine my label is not changing as it should, can someone tell me what I am doing wrong? code:
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)
}
}
Upvotes: 1
Views: 315
Reputation: 2093
the function timerLabel which creates your timerLbl is never called.
Upvotes: 1