当我打电话给[Timer isValid]或[Timer invalidate]时,NSTimer崩溃了

esa*_*its 10 iphone crash xcode objective-c nstimer

我的iPhone应用程序中有两个NSTimers.DecreaseTimer工作正常,但是当我调用[timerCountSeconds isValid]或[timerCountSeconds invalidate]时,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];
    }
}
Run Code Online (Sandbox Code Playgroud)

我做错了什么?你能帮我么?

Sha*_*rog 22

您应该将NSTimer对象设置为nil在您invalidate之后,因为invalidate方法调用也会执行release(根据Apple文档).如果不这样做,就像调用方法一样isValid可能会导致崩溃.