菜鸟教程小白 发表于 2022-12-12 08:52:59

ios - 在回调函数中调用 Objective-C 方法


                                            <p><div>
            <aside class="s-notice s-notice__info post-notice js-post-notice mb16"role="status">
      <div class="d-flex fd-column fw-nowrap">
            <div class="d-flex fw-nowrap">
                <div class="flex--item wmn0 fl1 lh-lg">
                  <div class="flex--item fl1 lh-lg">
                        <b>这个问题在这里已经有了答案</b>:
                        
                  <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您不能将 Objective-C 函数用作 C 函数的回调。</p>

<p>您可以做的是使用最后一个参数,旨在传递您想要的任何数据,将指针传递给您的类实例。</p>

<pre><code>AudioServicesAddSystemSoundCompletion (id,
                                       NULL,
                                       NULL,
                                       MyAudioServicesSystemSoundCompletionProc,
                                       self);
</code></pre>

<p>在 C 回调函数体内,将指针转换为您的类,然后使用它!</p>

<pre><code>void MyAudioServicesSystemSoundCompletionProc (SystemSoundID ssID, void *clientData);
{
    MyClass *obj = (MyClass*)clientData;
    // use obj in the normal Objective-C way!!!
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在回调函数中调用 Objective-C方法,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/22586472/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/22586472/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在回调函数中调用 Objective-C 方法