简体   繁体   English

另一种方法中的Objective-C使计时器无效

[英]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? 有人可以帮我吗?

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. 如果它们不在同一对象上,则应重新考虑您的设计,因为两个类正在尝试管理同一设施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM