views:

63

answers:

3

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;
}
A: 

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.

Georg Fritzsche
So you mean the only way to send multiple arguments upon a button click is to use collection object (dictionary, array etc) since they will always take only one argument?
Amarsh
@Amarsh: I mean you can't use incovations et al here and instead have to store the information and the relation to the button somewhere else.
Georg Fritzsche
@George: Yeah thanks. Your comment did help me solve my problem. Thans for that.
Amarsh
A: 

I solved it in a different way. Subclassed UIButton and added all the properties I needed to. This is how the class looks like:

@interface RecentSalePropertyDetailsButton : UIButton {
    Property* property;
}
@property (nonatomic, retain) Property* property;
@end
@implementation RecentSalePropertyDetailsButton
@synthesize property;
- (id) initWithPropertyAs:(Property*)aProperty{
    [super init];
    self.property = aProperty;
    return self;
}
@end

Then, further down, I do the following:

    // allocate the details button's action
    RecentSalePropertyDetailsButton* button = (RecentSalePropertyDetailsButton *)[myView viewWithTag:15];
    button.property = property;
    [button addTarget:self action:@selector(showRecentSalesPropertyDetails:) forControlEvents:UIControlEventTouchDown];
Amarsh
A: 
//make a button
    UIButton *button = [UIButton buttonWithType:0];
//set button size
    button.frame = CGRectMake(20,219,280,106);
//give it a color
    button.backgroundColor = [UIColor clearColor];
//add a method
    [button addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
//add it to the currentview 
    [self.view addSubview:button];
ssj