开发者

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
                  开发者_如何学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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜