2012-05-16 55 views
1

我試圖通過UILabelAudioServicesAddSystemSoundCompletion但我無法操縱completionCallback方法內的值。我使用ARC和Xcode建議添加(_bridge void*)傳遞參數與AudioServicesAddSystemSoundCompletion

任何幫助將不勝感激。

-(void) playWordSound:(UILabel *)label 
{ 
    NSString *path; 
    SystemSoundID soundId; 
    switch (label.tag) 
    { 
     case 1: 
      .......... 
      break; 
    } 
    NSURL *url = [NSURL fileURLWithPath:path]; 
    AudioServicesCreateSystemSoundID((CFURLRef)objc_unretainedPointer(url), &soundId); 
    AudioServicesPlaySystemSound(soundId); 
    AudioServicesAddSystemSoundCompletion (soundId, NULL, NULL, 
              completionCallback, 
              (__bridge void*) label); 
} 


static void completionCallback (SystemSoundID mySSID, void* data) { 
    NSLog(@"completion Callback"); 
    AudioServicesRemoveSystemSoundCompletion (mySSID); 
    //the below line is not working 
    //label.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; 
} 

回答

3

在完成處理程序中,標籤存儲在data中。你需要__bridge它回來使用它。

static void completionCallback (SystemSoundID mySSID, void* data) { 
    NSLog(@"completion Callback"); 
    AudioServicesRemoveSystemSoundCompletion (mySSID); 
    UILabel *label = (__bridge UILabel*)data; 
    label.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; 
} 
+0

非常感謝,它現在正在爲我工​​作。你能簡單地解釋一下noob背後的想法,以及它究竟做了什麼? – garethdn

+1

它是在ARC中引入的,用於在存儲到泛型類型(如void *)時處理重計數。請參閱[ARC和橋接轉換](http://stackoverflow.com/questions/7036350/arc-and-bridged-cast) – Joe

+0

喬,你能告訴我如何從'completionCallback'方法中調用另一個方法。在我更改文字顏色後,我嘗試用'[self anotherMethod]'調用另一個方法,但它不適用於我。 – garethdn