user3721424
user3721424

Reputation: 163

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.

Upvotes: 2

Views: 1679

Answers (3)

Rohit
Rohit

Reputation: 1174

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

Upvotes: 1

tomahh
tomahh

Reputation: 13651

Your timer is only used to fire the method that will update the time label.

You need to reset the variable that holds the elapsed time to zero when the game finishes. Probably timeSec in your case, which is hold by your GameScreen class.

Upvotes: 0

EDUsta
EDUsta

Reputation: 1933

You should reset your timeSec and timeMin variables when he decides to start a new game.

In other words, whenever that subview with the button is removed from its superview, reset it.

Or, do it into that button action.

Upvotes: 2

Related Questions