Luca
Luca

Reputation: 20959

calling selector with arguments

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

Answers (3)

Sree
Sree

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

Antonio MG
Antonio MG

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

Ian L
Ian L

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

Related Questions