Objective-C 在另一种方法中使计时器失效
我有一个方法,其中声明了一个计时器;
- (void)startTimer:(id)sender {
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval: 0.8
target: self
selector: @selector(toggleButtonImage:)
userInfo: nil
repeats: YES];
}
我想做的是,在另一种方法中,如果计时器正在运行,我想使其无效,这是我到目前为止所拥有的,但我收到错误“计时器未声明”
- (void)stopTimer:(id)sender {
if ( [timer isValid]) {
[timer invalidate], timer=nil;
}
}
有人可以帮助我吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果两种方法都在同一个控制器上,则只需将计时器设置为实例变量即可。如果它们不在同一个对象上,您应该重新考虑您的设计,因为两个类正在尝试管理相同的设施。
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.