Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an NSTimer to stop repeating after a condition is met [duplicate]

Tags:

ios

swift

nstimer

I have two NSTimers that I programmed to make a button appear on the screen and then disappear. How can I program it to stop adding and removing buttons once a condition is met?

Here is my code:

   var timerRemoveButton = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "removeButton", userInfo: nil, repeats: true)
   var timerAddButton = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "addButton", userInfo: nil, repeats: true)
like image 368
bhzag Avatar asked Jun 19 '14 20:06

bhzag


1 Answers

You can invalidate them as in usual Objective-C. So when your condition is met just write:

timerRemoveButton.invalidate()
timerAddButton.invalidate()

This will remove your timers from an NSRunLoop object.

like image 94
Oleksandr Karaberov Avatar answered Sep 28 '22 08:09

Oleksandr Karaberov