Iphone, callback and target c

I am using the C ++ library using a callback to report the progress of the operation. Everything works just fine, except for this: I need the function in my controller to be used as a C ++ callback function. I have tried many things, but none of them work.

Do you know how we can handle this?

Thank:)

+3
source share
3 answers

You must define a C ++ class in your .h using your callback methods, implementing the C ++ interface. This class also saves the delegate of your objC class.

.m @end ++. objC

.h

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

.m

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

API- iPhone, , void *, Objective-C.

++ - void * "context" - :

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

, inClientData , .

+5

There is a much better way to do this. If you're already using boost in your iOS project, you can use my objc_callback template: http://alex.tapmania.org/2012/04/c-to-obj-c-callbacks-part-2.html

+2
source

Source: https://habr.com/ru/post/1735810/


All Articles