无法使(停止)NSTimer失效 [英] Unable to invalidate (Stop) NSTimer
问题描述
可能重复:
NSTimer不会停止
Possible Duplicate:
NSTimer doesn't stop
我正在使用NSTimer在播放音频时更新滑块的值.
I am using a NSTimer to update the value of a slider while a audio is playing.
if (sliderUpdateTimer) {
[sliderUpdateTimer invalidate];
sliderUpdateTimer=nil;
}
sliderUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateSliderForAudio) userInfo:nil repeats:YES];
一旦音频播放完毕,我将使audioPlayer委托方法中的Timer无效.
once the audio is finished playing i am invalidating the Timer in audioPlayer delegate method.
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
NSLog(@"audioPlayerDidFinishPlaying");
[player release];
audioPlayer=nil;
[sliderUpdateTimer invalidate];
[sliderUpdateTimer release];
sliderUpdateTimer=nil;
}
正在调用委托方法,但计时器没有停止....我只有一个线程在其上运行计时器.但是计时器仍然没有停止...欢迎您提供任何帮助.
The delegate method is getting called but the timer is not stopping.... I have only once thread on which i am runing the timer. But still the timer does not stop.... Any help in this regard is welcome...
推荐答案
首先,您将过度释放它,并且应该收到EXC_BADACCESS(设置定时器时不会保留定时器,因此不应释放它要么).应该只拨打:
First of all, you would be over releasing it and should receive EXC_BADACCESS (you're not retaining the timer when you set it, so you shouldn't release it either). Should only call:
[sliderUpdateTimer invalidate];
sliderUpdateTimer=nil;
由于您没有收到崩溃的信息,因此在调用audioPlayerDidFinishPlaying:
时似乎sliderUpdateTimer
是nil
.用户NSLog或放置一个断点以检查是否为真.如果是这种情况,您可能会在某个时候将其设置为nil,搜索slideUpdateTimer
并检查可能在何处发生.
Since you don't receive a crash, it seems sliderUpdateTimer
is nil
when calling audioPlayerDidFinishPlaying:
. User NSLog or put a breakpoint to check if this is true. If this is the case, you're probably setting it to nil at some point, search for slideUpdateTimer
and check where this might occur.
这篇关于无法使(停止)NSTimer失效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!