views:

131

answers:

2

I have been trying to write a delegate to a view controller which has a method which will call back the sender when it is done

-(void) doSomething:(id)target action:(SEL)action object:(id)object{
  //Do Some work
  //Produce an NSArray* called array
  object = array;
  if([target respondsToSelector:action])
    {
      [target action];
    }
}

The idea being that the action method in the sender also has a reference to object and it can read the results and do something once the selector has been called to use the data.

The problem I am having is that [target respondsToSelector:action] returns true so the code tries to call the selector but then I get an SIGABRT signal and the message that NSObject -doesNotRegogniseSelector.

Does anyone know where I am going wrong?

+4  A: 

Try:

[target performSelector:action];

Typing [target action] is not how to dynamically call a method. It would be like calling [anObject @selector(doSomething)]. Also, it's probably not needed to check whether target responds to selector, since your method is being called with those as parameters, and it would be a mistake on the caller's part to pass in a bad selector.

Jason
Further to that i am setting the contents of object to an array, but that doesn't appear to have changed in the callback method... any tips?
Craig Warren
Setting `object = array` does nothing to the caller. It's like passing in `parameter:(int)x` and then setting `x = 42`. The caller has no way of knowing it's set to 42. You could pass in a NSMutableArray and then copy the objects from `array` to `object`.
Jason
+2  A: 

You should uses - (id)performSelector:(SEL)aSelector to call the selector.