【问题标题】:Passing arguments with AudioServicesAddSystemSoundCompletion使用 AudioServicesAddSystemSoundCompletion 传递参数
【发布时间】:2012-05-24 01:51:47
【问题描述】:

我正在尝试使用AudioServicesAddSystemSoundCompletion 传递UILabel,但我无法在completionCallback 方法中操作该值。我正在使用 ARC,Xcode 建议添加 (_bridge void*)

任何帮助将不胜感激。

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

【问题讨论】:

    标签: iphone objective-c ios ipad audio


    【解决方案1】:

    在完成处理程序中,标签存储在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];
    }
    

    【讨论】:

    • 非常感谢,它现在对我有用。您能否向菜鸟简要解释一下桥接背后的想法以及它的作用?
    • 它是在 ARC 中引入的,用于在存储到像 void* 这样的泛型类型时处理引用计数。见ARC and bridged cast
    • Joe,你能告诉我如何从 completionCallback 方法中调用另一个方法吗?更改文本颜色后,我尝试使用 [self anotherMethod] 调用另一种方法,但它对我不起作用。
    • 开始另一个问题并在此处发布链接。确保您检查它是否已被回答。
    猜你喜欢
    • 1970-01-01
    • 2011-04-22
    • 2013-10-14
    • 2012-04-28
    • 2016-05-25
    • 2015-08-12
    • 2014-01-21
    • 2011-08-10
    • 1970-01-01
    相关资源
    最近更新 更多