I'm trying to add timers to a NSRunLoop. My expected outcome is that once the timers have been added to the loop, they start counting down independent from one another.
My code now looks like this:
var timer = NSTimer()
let mainRunLoop:NSRunLoop = NSRunLoop()
func blurViewActive(gestureRecognizer:UIGestureRecognizer) {
if (gestureRecognizer.state == UIGestureRecognizerState.Began){
println("STATE BEGAN")
var point = gestureRecognizer.locationInView(self.tv)
if let indexPath = self.tv.indexPathForRowAtPoint(point){
let data = messageList[indexPath.row] as Messages
if let theCell = self.tv.cellForRowAtIndexPath(indexPath) as? TableViewCell{
self.timer = NSTimer(timeInterval: 1, target: self, selector: "updateCounter", userInfo: nil, repeats: true)
self.mainRunLoop.addTimer(timer, forMode: NSRunLoopCommonModes)
mainRunLoop.run()
}
}
}
}
var counter = 10
func updateCounter(){
if counter == 0{
timer.invalidate()
}else{
counter = --counter
println(counter)
}
}
Right now, nothing seems to happen when my button is pressed. My understanding is that once the timer has been added to the run-loop, it will start running independently.
Any suggestions on how this is done correctly would be appreciated.