开发者

Objective-C invalidate timer in another method

开发者 https://www.devze.com 2023-02-19 22:22 出处:网络
I have a method in which i have declared a timer; - (void)startTimer:(id)sender { NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval: 0.8

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

- (void)startTimer:(id)sender {
    NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval: 0.8
                                                     target: self
                  开发者_如何学Go                                 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消