0

I have this API that I cannot modify in MyViewController.m file:

void my_Callback (void* context, xxxxxx::eventType::type eventtype, int code, const myconst)
{

  //do stuff

}

I need to call this function:

-(void) update
{
  self.textbox.test =@"new text";
}

but when I try to do

void my_Callback (void* context, xxxxxx::eventType::type eventtype, int code, const myconst)
  {        
      //do stuff
      [self update];        
  }

I get error because it can't reconize "self".

I tryed to do:

ViewController *newview = [[Viewcontroller alloc]init];
        void my_Callback (void* context, xxxxxx::eventType::type eventtype, int code, const myconst)
        {

          //do stuff
          [newview update];

        }

and it finally goes into myfunction "update" but the new text is not set.

How can i get my text updated? Thanks

CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67
  • 3
    You have pass self as a parameter. Check also this thread: http://stackoverflow.com/questions/1280017/how-to-call-an-objective-c-method-from-a-c-method – Leijonien Nov 18 '13 at 12:31
  • The text didnt get updated when you tried by creating a new viewcontroller because, the textView you are updating is the self.textbox.text and you are calling the update function for the new viewcontroller. And about the cant recognize "self" error you got, may be because its a static method. – Ashok Nov 18 '13 at 12:41
  • "my_Callback" is a regular C method, and has no logical association with the other functions in the .m file containing it. If you want a pointer to any Objective-C object (including "self") you must pass the pointer as a parameter or somehow access it as a global value. – Hot Licks Nov 18 '13 at 15:48

2 Answers2

1

Who calls this callback ? Usually there's a way to put some "user data". In your case it seems to be the void *context argument to the callback.

So where you define the callback, you may have a chance to specify the context. There, you can pass it the actual "self", so that you can get it later.

Careful however, as doing so often results in using released instances sooner or later.

aspyct
  • 3,625
  • 7
  • 36
  • 61
  • Callback define: typedef void my_Callback (void* context, xxxxxx::eventType::type eventtype, int code, const myconst). Again, I cannot modify this. This is how Callback is called: MyManager->registerEvent ("My listener", &My_Callback, MyManager); – accountDevelopment Nov 18 '13 at 13:20
  • What is the signature of `MyManager->registerEvent` method? Probably its last argument is the context, in which case you can easily get the `self` from the first argument of your callback, the `context`. – aspyct Nov 18 '13 at 13:37
  • virtual Result registerEvent(const Stringname, My_Callback listener, void* context) =0 ; How should I modify this? thanks – accountDevelopment Nov 18 '13 at 13:51
  • You're probably fine then. Try to figure out what you receive as `context` in your `my_Callback` function. It's probably the `MyManager` object. – aspyct Nov 18 '13 at 15:07
-2

Please check if textbox label is initialized. There might be a possibility that textbox label is un initialized.

Itesh
  • 199
  • 8
  • 2
    Since his callback is a simple C function, not a part of a method (meaning: there's no such thing as `self` in that scope), `textbox` doesn't yet matter. – mah Nov 18 '13 at 12:38