Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-c iPhone NSTimer unique identifier

I've got a question about a NSTimer that i declared in the .h file and later set inside a method. But this method will be called more then ones, so the NSTimer runs multiple times under the same name.

Now my question is, is it possible to set a tag/id or whatever so i can invalidate the NSTimer with the correct tag/id?

Thanks for help!

like image 424
Flappy Avatar asked Dec 02 '25 21:12

Flappy


1 Answers

This question was asked long ago, but since I had the same need today, here's my solution:

// set timer
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                         target:self
                                       selector:@selector(whateverMethod)
                                       userInfo:@"timer1"
                                        repeats:YES];

To invalidate this same timer, check its userInfo string:

// invalidate timer
if ([[timer userInfo] isEqual:@"timer1"])
    [timer invalidate];
like image 135
lucasart Avatar answered Dec 04 '25 11:12

lucasart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!