NSTimer 不会停止 [英] NSTimer doesn't stop
本文介绍了NSTimer 不会停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个计时器,它在调用 viewWillAppear
方法时触发,并在调用 viewDidDisappear
方法时失效.但是在视图之间切换了一定数量后,即使在它无效之后,计时器仍会继续触发.有什么问题?
I've got a timer which fires when the viewWillAppear
method is being called and invalidates when the viewDidDisappear
method is being called. But after a certain amount of switching between views the timer continues firing even after it was invalidated. What's the problem?
这是我的代码:
NSTimer *timer;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
timer = [NSTimer scheduledTimerWithTimeInterval: 0.2f
target: self
selector:@selector( timerAction )
userInfo:nil
repeats:YES];
}
-(void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[timer invalidate];
timer = nil;
}
-(void) timerAction
{
NSLog(@"timerAction");
}
推荐答案
我应该通过保留它来保持指向计时器的链接.:)我 5 个月前问过这个问题,令人惊讶的是,我获得了多少经验.:)
I should have been keeping a link to the timer by retaining it. :) I've asked this question 5 months ago, and it's amazing, how much more experience I acquired. :)
timer = [ [NSTimer scheduledTimerWithTimeInterval: ...] retain];
...
...
[timer invalidate];
[timer release];
这篇关于NSTimer 不会停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文