Woodstock
Woodstock

Reputation: 22926

NSTimer Class Methods, and Invalidation

If I use:

[NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(myMethod) userInfo:nil repeats:YES]; 

How can I invalidate this, as it's a class method I don't have access to a pointer to it?

Upvotes: 0

Views: 145

Answers (2)

stosha
stosha

Reputation: 2148

You can make something like this:

[NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(myMethod:) userInfo:nil repeats:YES];

(note ":" after myMethod)

- (void) myMethod: (id) sender
{
    if ([sender isKindOfClass:[NSTimer class]])
    {
        NSTimer* timer = (NSTimer*) sender;
        [timer invalidate];
    }
}

Upvotes: 1

Chuck
Chuck

Reputation: 237060

It's a class method that returns a timer. You invalidate that timer.

Upvotes: 1

Related Questions