Aneesh
Aneesh

Reputation: 1733

NSInvalidArgumentFormatException caused by unrecognized selector was sent to sender : while trying to print tag of a UILabel

I have a ScrollView which has many blocks of data. Each block has a UILabel. I'm creating each of this block inside a for loop :

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];

The selector for the claim label is :

- (IBAction)claimClicked:(UILabel *)sender{
    NSInteger the_tag = ((UIView*)sender).tag;
    NSLog(@"%ld",the_tag);
}

Basically Im just printing the tag on clicking the label. But I get an exception saying :

NSInvalidArgumentFormatException caused by unrecognized selector was sent to sender on the NSlog line.

Help.

Upvotes: 0

Views: 54

Answers (2)

Mani
Mani

Reputation: 17585

You cann't do this definition - (IBAction)claimClicked:(UILabel *)sender. Because according to this line UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(claimClicked:)];, your sender is UITapGestureRecognizer.

Instead try this..

- (IBAction)claimClicked:(UIGestureRecognizer *)sender
{
   if([sender.view isKindOfClass:[UILabel class]])
      NSLog(@"Label tag %d",sender.view.tag);
}

Upvotes: 1

KudoCC
KudoCC

Reputation: 6952

Actually the sender here is UITapGestureRecognizer.

Using the code instead:

- (IBAction)claimClicked:(UIGestureRecognizer *)sender
{
    NSLog(@"%d",sender.view.tag);
}

Upvotes: 3

Related Questions