Iphone, callback and objective c

3

我正在使用一个C++库,使用回调函数来通知操作的进度。一切都很正常,除了这个问题:

我需要在我的控制器中有一个函数作为C++回调函数使用。我尝试了很多方法,但它们都没有起作用。

您知道我们如何处理这种情况吗?

谢谢 :)


请提供更具体的信息:您尝试了哪些方法? - Jon Reid
3个回答

5

iPhone API(应用程序接口)如音频队列服务(Audio Queue Services)在其回调中使用void *参数,您可以将Objective-C实例放入其中。

如果您的C++库具有类似的设置 - 您的回调提供一个void *"context"参数 - 您可以这样做:

void interruptionListener(void *inClientData, UInt32 inInterruptionState) {
  InterruptionMonitor *self = (InterruptionMonitor *)inClientData;
  [self inInterruption: inInterruptionState == kAudioSessionBeginInterruption];
}

所以您使用inClientData来存储您的实例,然后可以调用该实例上的方法来执行实际处理。

3

您需要在您的.h文件中定义一个C++类,其中包含您的回调方法,实现C++接口。该类还保留了您的ObjC类的委托。

在您的.m文件中,在@end之后指定C++方法。然后,您可以使用委托执行您的ObjC类的选择器。

在.h文件中

@interface YourObjcClass {
#ifdef __cplusplus
    class FooObserver : public YourNS::Interface {
    public:
        virtual ~FooObserver() {
        }
        YourObjcClass *delegate;
        };
YourNS::YourCallbackClass *myCallbackClass;
#endif

in .m

#ifdef __cplusplus
void FooObserver::callback( args ) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
[delegate performSelectorOnMainThread:@selector(performCallback) 
                   withObject:nil 
                waitUntilDone:false];
[pool release];
}
#endif

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接