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){

}
4
  • 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. Commented May 15, 2013 at 4: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? Commented May 15, 2013 at 4:04
  • 1
    See stackoverflow.com/questions/12193857/… Commented May 15, 2013 at 4:04
  • Maybe it is possible with objc_msgsend? stackoverflow.com/questions/2573805/… Commented May 15, 2013 at 8:16

1 Answer 1

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];

 }
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for the reply.do we have any trick to call a method from current class?
Your C Call back in ur Current Class?
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
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.
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 ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.