1

I want to callback an objective c method from c.

//c 
int gameEngineCallback(int buttontype,void (*callback)(void))

//using above function
gameEngineCallback(roundButton,callback);

//this works fine but I want to call objective c native method instead of this
static void callback(void){

}
Hassy31
  • 2,793
  • 3
  • 21
  • 37
  • You can't. The `callback` parameter needs a function with no parameters and a void return type. You can't pass an Objective-C method. – rmaddy May 15 '13 at 04:00
  • thanks.ok can I pass c method like above including objective-c objects? I mean simply how do I control UIbutton or webview through C callback method? – Hassy31 May 15 '13 at 04:04
  • 1
    See http://stackoverflow.com/questions/12193857/callback-methods-from-c-to-objective-c?rq=1 – rmaddy May 15 '13 at 04:04
  • Maybe it is possible with `objc_msgsend`? http://stackoverflow.com/questions/2573805/using-objc-msgsend-to-call-a-objective-c-function-with-named-arguments – hfossli May 15 '13 at 08:16

1 Answers1

1

You Can't Pass Objective c method in C CallBack protoType . but u can redirect to your Objective c Function from the C Call back definition.

EXAMPLE

//Add below line in public declaration of Your Ivar

id myclass; // if ur not Sure about ur class name or u have to redirect to ur delegate class

or 

YourClassName *instanceof class; //(no need to create instance just declartion) 

// before ur call back fires Assign value for ur IVAr in ur init definition

myclass = self ;
or
myclass = delegate;

// use above IVAR like this in Your C callback Function

static void callback(void)
{

[myclass Your Function];

 }
Vasu Ashok
  • 1,413
  • 3
  • 17
  • 37
  • thanks for the reply.do we have any trick to call a method from current class? – Hassy31 May 15 '13 at 04:54
  • Your C Call back in ur Current Class? – Vasu Ashok May 15 '13 at 05:08
  • then in ur .m before @implementation declare ur ivar like `YourClassName *myclass`;(no need to create instance just declartion) and in viewdidload assign `myclass=self;` and then call function using myclass instead of 'self' inside ur Callback – Vasu Ashok May 15 '13 at 05:16
  • How about for void * I tried to do this yesterday and closest I got would tell me there was no method by the name visible in class. – uchuugaka May 15 '13 at 06:40
  • Hello Guys, I am trying to do the same. But, not able to get it. What I want to do is, I have a callback function which is static void. In this static callback function, I need to access my local variables and local methods. But I am not getting an access to it. Will you please help me to out to get this ? – Sid Dec 18 '13 at 11:33