我有一个 ScrollView
,它有很多数据 block 。每个 block 都有一个 UILabel
。我在 for 循环中创建每个 block :
for(int i = 0 ; i < x ; i++){
UILabel *claim = [[UILabel alloc]initWithFrame:CGRectMake(40,135, self.view.bounds.size.width, 30)];
claim.text = @"claim";
[claim setTag:i];
NSLog(@"%ld",claim.tag);
claim.font = [UIFont fontWithName:@"Arial" size:(20.0)];
[claim setTextColor:[UIColor purpleColor]];
//create a tab recogniser for claim text
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(claimClicked:)];
[claim setUserInteractionEnabled:YES];
[claim addGestureRecognizer:tap];
[scrollView addSubView :claim];
}
[self.view addSubView : scrollView];
声明标签的选择器是:
- (IBAction)claimClicked:(UILabel *)sender{
NSInteger the_tag = ((UIView*)sender).tag;
NSLog(@"%ld",the_tag);
}
基本上我只是在点击标签时打印标签。但我得到一个异常(exception)说:
NSInvalidArgumentFormatException caused by unrecognized selector was sent to sender
在 NSlog 行上。
帮助。
最佳答案
实际上这里的发送者是UITapGestureRecognizer。
改用代码:
- (IBAction)claimClicked:(UIGestureRecognizer *)sender
{
NSLog(@"%d",sender.view.tag);
}
关于ios - 无法识别的选择器导致的 NSInvalidArgumentFormatException 已发送至发件人 : while trying to print tag of a UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20966093/