从C函数回调访问一个ObjC对象 [英] Accessing an ObjC object from a C function callback

查看:144
本文介绍了从C函数回调访问一个ObjC对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我玩弄在全球碳热键了一下,我注册了一个热键。当热关键是pressed,这个函数被调用:

I'm playing around a bit with global hot keys in Carbon, and I registered a hot key. When the hot key is pressed, this function is called:

OSStatus myHotKeyHandler(EventHandlerCallRef nextHandler, EventRef anEvent, void *userData) {
    ...
}

这个功能是在同一个文件作为我的应用程序委托的定义。如何我现在请我在该函数的委托方法ObjC?

This function is in the same file as my app delegate's definition. How do I now call an ObjC method on my delegate in that function?

[self aMethod];

我不能添加一个无效* (是一个指向我的类的实例)的C函数,因为我不是一个调用它。

I cant add a void* (to be a pointer to my class instance) to the C function, because I am not the one calling it.

我试过的方法转换成一个类的方法,然后用叫它[AppDelegate中机能缺失] ,它的作品,但后来我不能用我的实例变量!

I tried to convert the method into a class method and then call it with [AppDelegate aFunction], which works but then I cant use my instance variables!

推荐答案

当您注册的事件处理程序提供指针到您的类实例作为参数用户数据。然后,您将得到这个指针返回的处理程序中的用户数据

Give the pointer to your class instance as parameter for userData when you register your event handler. You will then get this pointer back inside the handler as userData.

这篇关于从C函数回调访问一个ObjC对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆