2012-03-15 68 views
0

我有一个方法:带参数调用选择

-(void)pickTheQuiz:(id)sender etat:(int)etatQuiz{ 
    //i need to get the etatQuiz value here  


} 
在我的代码

和地方,我调用上面的方法是这样的:

int etat_quiz=4; 
[button addTarget:self action:@selector(pickTheQuiz:etat:)withObject:etat_quiz forControlEvents:UIControlEventTouchUpInside]; 

在这样做时,我得到错误:

Receiver type UIButton for instance message does not declare a method with selector 'addTarget:action:withObject:forControlEvent 

编辑:

-(void)pickTheQuiz:(id)sender etat:(int)etatQuiz{ 


    NSLog(@"The part number is:%i",((UIControl*)sender).tag); 

} 

回答

3

你不能像这样传递参数。而不是你可以设置“etatQuiz”作为你的标签你的按钮。并可以在你的选择器中访问它。 例如:

button.tag = etatQuiz; 
[button addTarget:self action:@selector(pickTheQuiz:) forControlEvents:UIControlEventTouchUpInside]; 


-(void)pickTheQuiz:(UIButton *)sender { 
    int value = sender.tag; 


} 
+0

如果我想传递多个信息?例如2个标签。 thnx提前。 – Luca 2012-03-15 09:13:25

+0

请参阅http://stackoverflow.com/questions/3716633/passing-parameters-on-button-actionselector此链接 – Sree 2012-03-15 09:19:10

0

最简单的解决方案是将整数变量作为按钮的tag属性添加。然后,您可以使用((UIButton *) sender).tag来检索它;

button.tag = 4; 
[button addTarget:self action:@selector(pickTheQuiz:) forControlEvents:UIControlEventTouchUpInside]; 

然后在pickTheQuiz:方法:

int etatQuiz = ((UIButton *) sender).tag; 
2

您使用addTarget错了,你应该做的:

addTarget:self action:@selector(pickTheQuiz:) forControlEvents:(UIControlEventTouchUpInside) 

而在按钮的标签添加变量,例如

+0

我预计':'编译时使用你的解决方案,为什么使用'::'? – Luca 2012-03-15 09:01:23