objective-c - 使用参数调用选择器

标签 objective-c ios

我有一个方法:

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

}

最佳答案

您不能像这样传递参数。相反,您可以将“etatQuiz”设置为按钮的标签。并且可以在选择器中访问它。 例如:

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


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


}

关于objective-c - 使用参数调用选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9716465/

相关文章:

objective-c - 如何使用 MKAnnotation

objective-c - 更改文件大小而不损失质量 iOS

ios - 从其他 ViewController 中删除带有标签的 View

ios - 在横向模式下使用 iPad 时,有没有办法在 ios 应用程序中获取滑入式菜单?

captureOutput 中的 iOS 应用相机方向

ios - 如何访问这些字典值? ( swift )

ios - 在 iPhone 上将照片保存到磁盘的最有效内存方式?

ios - 每个部分的 UICollectionView 布局

objective-c - 带有自动布局的 UIScrollView 内的 UITextView

iphone - 我可以编码 NSManagedObject 的子类吗?