2011-10-25 20 views
2
-(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]; 

} 

cancelPreviousPerformRequestsWithTarget:selfdealloc工作叫,但不工作時,選擇仍可以調用。但cancelPreviousPerformRequestsWithTarget:selfvoiceBroadcast沒有問題。 cancelPreviousPerformRequestsWithTarget方法不能用於dealloccancelPreviousPerformRequestsWithTarget不dealloc的

回答

5

如果您正在使用的UIViewController ...

我不知道是不是執行爲什麼,但你可以嘗試把

[NSObject cancelPreviousPerformRequestsWithTarget:self]; 

-viewDidUnload方法,這是在dealloc之前調用。在UIViewController's reference

+0

它不工作,這種方法「[NSObject的cancelPreviousPerformRequestsWithTarget:個體經營]。不執行,但感謝你的幫助,畢竟 – Fannic

+0

也許theese方法('-dealloc' ,'-viewDidUnload')沒有被執行?把一些NSLogs放在那裏,告訴我是否在控制檯中有輸出。 – akashivskyy

+0

dealloc被執行,並且cancelPreviousPerformRequestsWithTarget:self在dealloc中也被執行,但它不起作用。 viewDidUnload不會被執行 – Fannic

1

更多的信息,我從未有過

[NSObject cancelPreviousPerformRequestsWithTarget:(id)]

任何成功所以不是我用

[NSObject cancelPreviousPerformRequestsWithTarget:(id) selector:(selector) object:(id)]

唯一的缺點是,你必須有一個請向後者致電您之前設置的每個延遲演奏。

希望能幫助別人。不知道爲什麼第一個似乎不適合我。

0

您已在performSelector方法中將nil作爲參數傳遞,並使用cancel作爲自變量的參數。在參數中使用不同的對象會導致問題。

使用此,

[self performSelector:@selector(SampleMethod) withObject:self afterDelay:delayTime]; 
[NSObject cancelPreviousPerformRequestsWithTarget:self];