0

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);

}
Luca
  • 20,399
  • 18
  • 49
  • 70

3 Answers3

3

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;


}
Sree
  • 907
  • 5
  • 14
  • and if i want to pass more than one information? for example 2 tags. thnx in advance. – Luca Mar 15 '12 at 09:13
  • Please refer http://stackoverflow.com/questions/3716633/passing-parameters-on-button-actionselector this link – Sree Mar 15 '12 at 09:19
2

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

Antonio MG
  • 20,382
  • 3
  • 43
  • 62
0

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;
Ian L
  • 5,553
  • 1
  • 22
  • 37