-1

大家好,我需要这个代码在这种情况下,第一个参数是新闻,第二个AIF u能做到这一点转化为函数有两个参数?转换代码的功能

-(IBAction)news 
{ 
CFURLRef  soundFileURLRef; 
SystemSoundID soundFileObject; 
CFBundleRef mainBundle; 
mainBundle = CFBundleGetMainBundle(); 

// Get the URL to the sound file to play 
soundFileURLRef = CFBundleCopyResourceURL (mainBundle, 
              CFSTR ("News"), 
              CFSTR ("aif"), 
              NULL); 

// Create a system sound object representing the sound file 
AudioServicesCreateSystemSoundID (soundFileURLRef, &soundFileObject); 

AudioServicesPlaySystemSound (soundFileObject); 

return; 

}

回答

3

如果CFSTR的就是这个问题,那么解决的办法是:

-(void) playNews: (NSString*) news type: (NSString*) type 
{ 
    CFURLRef  soundFileURLRef; 
    SystemSoundID soundFileObject; 
    CFBundleRef mainBundle; 
    mainBundle = CFBundleGetMainBundle(); 

    // Get the URL to the sound file to play 
    soundFileURLRef = CFBundleCopyResourceURL (mainBundle, 
      (CFStringRef)news, 
      (CFStringRef)type, 
      NULL); 

    // Create a system sound object representing the sound file 
    AudioServicesCreateSystemSoundID (soundFileURLRef, &soundFileObject); 

    AudioServicesPlaySystemSound (soundFileObject); 
} 

(IBAction为)声明使得这里没有任何意义,因为这种方法不能直接从Interface Builder中使用。

+0

你好,谢谢你的回答,这是我想要的,但是当我写PlayNews时:@“news1”ofType:@“wav”; Xcode给我一个错误:预期';'在'type'之前,你能帮我解决这个问题 – 2010-10-04 20:24:53

+1

你需要把它放在尖括号中:[obj playNews:@“news”ofType:@“wav”]; – Codo 2010-10-04 21:17:10

0

如果这意味着要使用控制从用户的一类接收了一个动作,那么你可以做的唯一事情是-(IBAction)nameOfAction:(id)sender;。否则,您需要编写命令以获取您需要的两个参数,并从IBAction中调用该参数。