views:

51

answers:

1

Hi all,

I am a little perplexed and I have been working on this for hours and googling without any real leads. I want to create a callback in objective-c for my iPhone app utilizing the @selector.

Class 1:

- (void) someMethod {    
   // create selector
   SEL successCallback = @selector(successMethod);

   // call some service with caller and selector
   [class2 dispatchSomeEvent:self callback:successCallback];

   // here's the call back method
   - (void) successMethod {
      NSLog(@"Callback success");
   }
}

Class 2:

// some event
- (void) dispatchSomeEvent:(id) caller selector:(SEL) successCallback {
   // catch the event and execute callback
   if ([caller respondsToSelector:successCallback]) {
      [caller successCallback];
   } 
}

The conditional respondsToSelector will pass but the callback on the next line will fail. HOWEVER, if I would do like this:

// catch the event and execute callback
if ([caller respondsToSelector:successCallback]) {
   [caller successMethod];
}

So instead of using the selector I passed, I type in the method name directly... and it works!

The error I get is this:

unrecognized selector sent to instance 0x6c37f70

What is going on here??

Thanks in advance!

+3  A: 

You should call your selector using -performSelector method:

if ([caller respondsToSelector:successCallback]) {
  [caller performSelector:successCallback];
} 
Vladimir
Never mind. I was being dense. :)
bbum
Thank you Vladimir.
Bjorn Harvold