Reputation: 99
-(IBAction)voiceBroadcast
{
if ([audioPlayer isPlaying]) {
[audioPlayer stop];
[NSObject cancelPreviousPerformRequestsWithTarget:self];
}
else {
if (nil != audioPlayer) {
[audioPlayer play];
audioPlayer.volume = 0.0;
[self performSelector:@selector(doBgMusicFadeIn)];
[self performSelector:@selector(doBgMusicFadeDown) withObject:nil afterDelay:3];
[self performSelector:@selector(startVoiceBroadcast) withObject:nil afterDelay:4];
}
}
}
-(void)dealloc
{
//position
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[super dealloc];
}
The cancelPreviousPerformRequestsWithTarget:self
in dealloc
is called, but does not work, the selectors still be called. But cancelPreviousPerformRequestsWithTarget:self
in voiceBroadcast
have no problem. Does the cancelPreviousPerformRequestsWithTarget
method could not be used in dealloc
?
Upvotes: 2
Views: 6241
Reputation: 141
You have passed nil as an argument in performSelector method and have used cancelPrevious method with self as an argument. The use of different objects in parameter is causing the problem.
use this,
[self performSelector:@selector(SampleMethod) withObject:self afterDelay:delayTime];
[NSObject cancelPreviousPerformRequestsWithTarget:self];
Upvotes: 1
Reputation: 5095
I have never had any success with
[NSObject cancelPreviousPerformRequestsWithTarget:(id)]
So instead I use
[NSObject cancelPreviousPerformRequestsWithTarget:(id) selector:(selector) object:(id)]
The only downside is that you must have one call to the latter for every delayed perform you have setup previously.
Hope that helps somebody. No idea why the first doesn't seem to work for me.
Upvotes: 1
Reputation: 45210
If you are using UIViewController...
I don't know why isn't it executed, but you can try putting
[NSObject cancelPreviousPerformRequestsWithTarget:self];
in -viewDidUnload
method, which is called before dealloc.
More info in UIViewController's reference
Upvotes: 5