I am trying to add a button programatically in a way that upon pressing it, a certain object is being passed. I keep on getting "unrecognized selector sent" exception. Can you suggest whats wrong with the code:
// allocate the details button's action
SEL selector = @selector(showPropertyDetails:forProperty:);
NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:selector];
//The invocation object must retain its arguments
[property retain];
//Set the arguments
[invocation setTarget:self];
[invocation setArgument:&property atIndex:3];
[(UIButton*)[myView viewWithTag:15] addTarget:self action:@selector(selector) forControlEvents:UIControlEventTouchDown];
and further down, the method in the same class looks like:
-(void) showPropertyDetails:(id)something forProperty:(Property *)property {
int i=0;
}
While you build a NSInvocation
, you are not using it anywhere - you are just setting the selector
as the action
for the button. This selector is expected to have a form like - (void)foo:(id)sender
, ....
You could instead use a dictionary with e.g. the tag as a key that maps to a certain NSInvocation
or stores additional arguments.