【问题标题】:How to link to a delegate within a C block of code如何链接到 C 代码块中的委托
【发布时间】:2014-05-05 15:51:14
【问题描述】:

我的可可视图控制器中有 C 代码和 Objective-C 代码。我目前通过使用全局 self 变量引用objective-c self 来链接两者,然后我使用该变量在C 中访问。

当我想从我的 C 代码块中调用一个 Objective-C 方法时,这很有效。 但现在我正试图通过我的 c 代码访问一个属性,比如[selfGlobal delegate],但这在运行时返回 null。

引用我的委托属性的正确方法是什么?

这是我的代码和调试器输出,显示了我的 Objective-C 块中代码的预期输出,并且当我尝试在 C 中建立到我的委托的链接时还显示了意外的输出,请记住,这在以下情况下确实有效我调用了一个方法,但当我尝试链接到委托属性时没有调用。

记录预期输出的 Objective-C 代码块:

-(id)initWithDelegate:(id)aDelegate withWindow:(NSWindow*)aWindow{
    self = [super init];
    if(self){
        delegate = aDelegate;
        NSLog(@"delegate: %@", delegate);

        window = aWindow;
    }
    return self;
}

以下是我在 Objective-C 方法中调用委托方法的方法:

SEL selector = @selector(PKFPSetProgressMaxCount:);
if ([delegate respondsToSelector:selector]) {
    [delegate PKFPSetProgressMaxCount:maxEnrollCount];
}

下面是我尝试在后台线程中运行的 C 函数中链接到我的委托的方法。我也尝试在调试时注销委托属性的值

 NSLog(@"enroll, selfglobal delegate: %@", [selfGlobal delegate]);
if([selfGlobal delegate]){
    NSLog(@"delegate is not empty");
}
else{
    NSLog(@"delegate is empty");
}

SEL selector = @selector(PKFPDidReceiveActionMessage:);
if ([[selfGlobal delegate] respondsToSelector:selector]) {
    NSLog(@"delegate does have PKFPDidReceiveActionMessage in it");

    [[selfGlobal delegate] PKFPDidReceiveActionMessage:@"Say a word"];
}
else{
    NSLog(@"delegate does not have PKFPDidReceiveActionMessage in it");

}

这是我在调试器中得到的输出。

//Values from ref 1
init method called
window did load method called
delegate: <MainScreenWC: 0x610000124c40>

init device method called

//Ref 2
-[MainScreenWC PKFPSetProgressMaxCount:]

 //These are calls made moments before the c function is called in the
-[MainScreenWC enrollUser:]
-[MainScreenWC PKFPSetUIControlsEnabled:] enableUIControls: 0

 //Ref 3 - This is inside the C function, and the delegate is returning null
 enroll, selfglobal delegate: (null)
 delegate is empty
 delegate does not have PKFPDidReceiveActionMessage in it

有人可以帮忙吗?

更新 1

id selfGlobal; 设置在创建协议的同一文件中。

这是我试图从内部访问委托的 C 函数。

void FTR_CBAPI UserCallBack(
                            FTR_USER_CTX Context,
                            FTR_STATE StateMask,
                            FTR_RESPONSE *pResponse,
                            FTR_SIGNAL Signal,
                            FTR_BITMAP_PTR pBitmap)
{
    //This method is automatically called by the API that I am working with. 
    //I need to have access to the Objective-C delegate variable from within this code block, but the delegate is returning null only in here.
    ...

}

【问题讨论】:

  • 请复制您的代码,而不是显示屏幕截图。
  • 看看你如何开始你的话题也会很有帮助。考虑到线程可以接收参数,您决定使用全局变量,这让我有点惊讶。
  • 我正在使用全局变量,因为 C 函数是来自 API 的回调函数,因此更改变量 - 我敢肯定 - 将阻止回调函数被调用。
  • 为什么这会被否决?
  • 好吧好吧伙计们,天哪,我不知道上传图片有那么糟糕。抱歉,我已经更新了帖子,现在请撤消那些反对票,这样我们才能再次上路。

标签: objective-c c macos cocoa


【解决方案1】:

原来我只需要手动合成委托属性,由于某种原因它不喜欢自动合成。如果有人能解释为什么会这样,请编辑这篇文章以添加更多信息。

我现在得到了正确的输出:

delegate is not empty
delegate does have PKFPDidReceiveActionMessage in it
-[MainScreenWC PKFPDidReceiveActionMessage:] actionMessage: Say a word

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    • 2012-07-05
    • 2013-01-30
    • 1970-01-01
    相关资源
    最近更新 更多