Search code examples
iosobjective-cswiftnstimer

timer.invalidate() not working on same view in Swift


I am using Swift to develop an app that uses timer so i am using this code for timer

var timer = NSTimer()
timer=NSTimer.scheduledTimerWithTimeInterval(1.0,target:self,selector:Selector("updateTimeLabel"), userInfo:nil , repeats: true)

my problem is this when this game complete a view appears as subview of current view and the timer invalidates using

timer.invalidate()

and on this subview there is a button to start a new game when this button is pressed a new game is started but the timer starts from the time it stopped.

suppose the game finished in 00:10 time so the subview appears and if we start new game then the time will start from 00:11. This works fine when user moves to another view and then comes back to the same view for game play, in this case the timer starts from 00:00 pls tell where i am wrong

THANKS IN ADVANCE.


Solution

  • reset your sec and minute variable that are updating the label in your updateTimeLabel method. this would do