Reputation: 20959
I have a method:
-(void)pickTheQuiz:(id)sender etat:(int)etatQuiz{
//i need to get the etatQuiz value here
}
and somewhere in my code, i call the method above like this:
int etat_quiz=4;
[button addTarget:self action:@selector(pickTheQuiz:etat:)withObject:etat_quiz forControlEvents:UIControlEventTouchUpInside];
When doing so, i got error:
Receiver type UIButton for instance message does not declare a method with selector 'addTarget:action:withObject:forControlEvent
EDIT:
-(void)pickTheQuiz:(id)sender etat:(int)etatQuiz{
NSLog(@"The part number is:%i",((UIControl*)sender).tag);
}
Upvotes: 0
Views: 2682
Reputation: 907
You can't pass the argument like this.Instead of that you can set the "etatQuiz" as the tag of your button.And can access it in your selector. eg:
button.tag = etatQuiz;
[button addTarget:self action:@selector(pickTheQuiz:) forControlEvents:UIControlEventTouchUpInside];
-(void)pickTheQuiz:(UIButton *)sender {
int value = sender.tag;
}
Upvotes: 3
Reputation: 20410
You are using addTarget wrong, you should do:
addTarget:self action:@selector(pickTheQuiz:) forControlEvents:(UIControlEventTouchUpInside)
And add the variable for example in the tag of the button
Upvotes: 2
Reputation: 5601
Simplest solutions is to add the integer variable as the button's tag
property. You can then retrieve it by using ((UIButton *) sender).tag
;
button.tag = 4;
[button addTarget:self action:@selector(pickTheQuiz:) forControlEvents:UIControlEventTouchUpInside];
Then in pickTheQuiz:
method:
int etatQuiz = ((UIButton *) sender).tag;
Upvotes: 0