Sami
Sami

Reputation: 1404

Objective-C invalidate timer in another method

I have a method in which i have declared a timer;

- (void)startTimer:(id)sender {
    NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval: 0.8
                                                     target: self
                                                   selector: @selector(toggleButtonImage:)
                                                   userInfo: nil
                                                    repeats: YES];  
}

What i want to do is, in another method i want to invalidate the timer if it is running, here's the what i have so far but i get the error 'timer is undeclared'

- (void)stopTimer:(id)sender {
    if ( [timer isValid]) {
      [timer invalidate], timer=nil;
    }
}

Could anyone help me?

Upvotes: 0

Views: 1677

Answers (1)

user23743
user23743

Reputation:

If both methods are on the same controller, then simply make the timer an instance variable. If they are not on the same object, you should rethink your design as two classes are trying to manage the same facility.

Upvotes: 3

Related Questions