本文介绍了Nstimer崩溃,当我调用[Timer isValid]或[定时器无效]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iPhone应用程序中有两个NSTimers。
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?

推荐答案

你应该设置一个 NSTimer 对象 nil 之后 invalidate ,因为 invalidate 方法调用也有一个发布(根据苹果文档)。如果不这样做,调用 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.

这篇关于Nstimer崩溃,当我调用[Timer isValid]或[定时器无效]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 20:40