-1

I have a UITableView with cells that contain a UILabel. When I tap the UILabel, I'd want to execute an action, therefore I added a UITapGestureRecognizer.

UILabel *telephone = (UILabel *)[cell.contentView viewWithTag:420];
telephone.userInteractionEnabled = YES;
UITapGestureRecognizer *tapToCall = [[UITapGestureRecognizer alloc] initWithTarget:telephone action:@selector(tapToCall:)];
[telephone addGestureRecognizer:tapToCall];

Then I defined the selector method:

-(void)tapToCall: (UITapGestureRecognizer*) sender {
    UILabel *telephone = (UILabel *) sender.view;
    NSLog(@"%@", telephone.text);
}

But now I receive an error when I touch the UILabel:

2017-03-07 13:17:49.220 [37354:2794848] -[UILabel tapToCall:]: unrecognized selector sent to instance 0x7fc39f459250

2017-03-07 13:17:49.253 [37354:2794848] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILabel tapToCall:]: unrecognized selector sent to instance 0x7fc39f459250'

What did I do wrong here?

jbehrens94
  • 2,356
  • 6
  • 31
  • 59

3 Answers3

2

change the target from initWithTarget:telephone (not for the particular Control)

UITapGestureRecognizer *tapToCall = [[UITapGestureRecognizer alloc] initWithTarget:telephone 

to initWithTarget:self (need to invoke in current class)

UITapGestureRecognizer *tapToCall = [[UITapGestureRecognizer alloc] initWithTarget:self

full answer

UILabel *telephone = (UILabel *)[cell.contentView viewWithTag:420];
UITapGestureRecognizer *tapToCall = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToCall:)];
telephone.userInteractionEnabled = YES;
[telephone addGestureRecognizer:tapToCall];
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
1

Change like this

UITapGestureRecognizer *tapToCall = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToCall:)];
[telephone addGestureRecognizer:tapToCall];
karthikeyan
  • 3,821
  • 3
  • 22
  • 45
0

It should be like this

UILabel *telephone = (UILabel *)[cell.contentView viewWithTag:420];

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer   alloc] initWithTarget:self action:@selector(handleTap:)];

[tapRecognizer setDelegate:self];
[tapRecognizer setNumberOfTapsRequired:1];

[telephone addGestureRecognizer:tapRecognizer];

 - (void)handleTap: (UITapGestureRecognizer*) sender {

    UILabel *telephone = (UILabel *) sender.view;

    NSLog(@"%@", telephone.text);
    NSLog(@"%ld", (long)telephone.tag);

     switch(telephone.tag) {
        case 0: { }
            break;
        case 1: { }
            break;
    }
}
koen
  • 5,383
  • 7
  • 50
  • 89
Rajesh Dharani
  • 285
  • 4
  • 20