当我调用 [Timer isValid] 或 [Timer invalidate] 时,NSTimer 崩溃 [英] NSTimer crashes, when I call [Timer isValid] or [Timer invalidate]
问题描述
我的 iPhone 应用中有两个 NSTimer.DecreaseTimer 工作正常,但是当我调用 [timerCountSeconds isValid] 或 [timerCountSeconds invalidate] 时 TimerCountSeconds 崩溃.它们是这样使用的:
I have two NSTimers in my iPhone app. DecreaseTimer works fine, but TimerCountSeconds crashes when I call [timerCountSeconds isValid] or [timerCountSeconds invalidate]. They are used like this:
-(id)initialize { //Gets called, when the app launches and when a UIButton is pressed
if ([timerCountSeconds isValid]) {
[timerCountSeconds invalidate];
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //Gets called, when you begin touching the screen
//....
if ([decreaseTimer isValid]) {
[decreaseTimer invalidate];
}
timerCountSeconds = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(runTimer) userInfo:nil repeats:YES];
//....
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {//Gets called, when you stop touching the screen(not if you press the UIButton for -(id)initialize)
//...
decreaseTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(decrease) userInfo:nil repeats:YES];
//...
}
-(void)comept3 { //Gets calles when you rubbed the screen a bit
if ([timerCountSeconds isValid]) {
[timerCountSeconds invalidate];
}
}
我做错了什么?你能帮帮我吗?
What did I do wrong? Can you please help me?
推荐答案
你应该在 invalidate
之后设置一个 NSTimer
对象为 nil
,因为 invalidate
方法调用也执行 release
(根据 Apple 文档).如果你不这样做,调用类似 isValid
之类的方法可能会导致崩溃.
You should set an NSTimer
object to nil
after you invalidate
it, since the invalidate
method call also does a release
(as per the Apple docs). If you don't, calling a method on it like isValid
could cause your crash.
这篇关于当我调用 [Timer isValid] 或 [Timer invalidate] 时,NSTimer 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!