ios - 无法识别的选择器导致的 NSInvalidArgumentFormatException 已发送至发件人 : while trying to print tag of a UILabel

标签 ios objective-c

我有一个 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/

相关文章:

iOS - 如何找到与给定 CGRect 具有相同高度的正确字体大小(以磅为单位)?

sql - 将 NSPredicate 转换为 SQL

ios - UITableView 单元格在调整大小后不响应输入

android - Flutter - setState() 没有更新按钮的文本颜色?

ios - 未找到有效的签名身份,配置文件

ios - 使用透明的 UIImageView 来裁剪其他 UIImageView

iphone - 如何在 iOS 中将参数传递给 UIPopovercontroller

iOS PDF 原生搜索

ios - UIButton - 默认状态下的图像,选中状态下的标题

objective-c - 如何处理NSXMLParser中另一个标签内的标签