2010-09-13 23 views
0

J'essaie d'ajouter un bouton de manière programmatique de telle manière qu'en appuyant sur celui-ci, un certain objet soit passé. Je continue d'obtenir l'exception "sélecteur non reconnu envoyé". Pouvez-vous suggérer ce qui est erroné avec le code:UIButton avec NSInvocation

// 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]; 

et plus bas, la méthode dans la même classe ressemble à:

-(void) showPropertyDetails:(id)something forProperty:(Property *)property { 
int i=0; 
} 

Répondre

1

Alors que vous construisez un NSInvocation, vous ne l'utilisez pas partout - vous Réglez simplement le selector en tant que action pour le bouton. Ce sélecteur devrait avoir une forme similaire à - (void)foo:(id)sender, ....

Vous pouvez utiliser un dictionnaire avec, par exemple, l'étiquette en tant que clé qui correspond à un certain NSInvocation ou stocke des arguments supplémentaires.

+0

Donc vous voulez dire le seul moyen d'envoyer plusieurs arguments sur un bouton clic pour utiliser l'objet de collection (dictionnaire, tableau etc) car ils prendront toujours un seul argument? – Amarsh

+0

@Amarsh: Je veux dire que vous ne pouvez pas utiliser incovations et al ici et à la place devez stocker l'information et la relation au bouton ailleurs. –

+0

@George: Ouais merci. Votre commentaire m'a aidé à résoudre mon problème. Thans pour ça. – Amarsh

1

Je l'ai résolu d'une manière différente. Sous-classé UIButton et ajouté toutes les propriétés dont j'avais besoin. Voici comment la classe ressemble à:

@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 

Puis, plus bas, je fais ce qui suit:

// allocate the details button's action 
    RecentSalePropertyDetailsButton* button = (RecentSalePropertyDetailsButton *)[myView viewWithTag:15]; 
    button.property = property; 
    [button addTarget:self action:@selector(showRecentSalesPropertyDetails:) forControlEvents:UIControlEventTouchDown]; 
0
//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];