将代码转换为函数

时间:2010-10-04 19:16:27

标签: iphone objective-c cocoa-touch programming-languages audiotoolbox

大家好我需要将此代码转换为具有2个参数的函数,在这种情况下,第一个参数是新闻,第二个 aif 可以这样做吗?

-(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;

}

2 个答案:

答案 0 :(得分: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中使用。

答案 1 :(得分:0)

如果这是一个类使用控件的用户收到的操作,那么你唯一能做的就是-(IBAction)nameOfAction:(id)sender;。否则,您需要编写命令以获取所需的两个参数,并从IBAction调用该参数。