我的iPhone应用程序中有两个NSTimers。DecreaseTimer运行良好,但当我调用TimerCountSeconds isValid或timerCountSeconds isValid时,timerCountSeconds会崩溃。它们的用法如下:
-(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];
}
}我做错什么了?你能帮帮我吗?
发布于 2010-09-12 04:10:29
您应该在对NSTimer对象执行invalidate之后将其设置为nil,因为invalidate方法调用也会执行release (根据苹果文档)。如果不这样做,在它上面调用像isValid这样的方法可能会导致崩溃。
发布于 2010-09-12 04:08:16
存储在该变量中的计时器很可能已经被释放。如果你想让它存在很长一段时间,你需要保留它。
发布于 2012-10-06 20:02:33
[objTimer retain];那么它就不会在任何时候崩溃。在初始化计时器之后使用这个,这样它就会工作得很好...
https://stackoverflow.com/questions/3692359
复制相似问题