0

I'm trying to create a tableview that the cells contains name, telephone and mail. If the user touches the telephone, the app calls to the phone (click to call). If the user touches the mail, I need to open the mail app with a new email to the "touched address".

First, I'm trying to develop the touchable label that contains an email, but I'm receiving the error "selector sent to instance".

I need to identify the tapgesture, and then, identify if it's an email or telephone, get the text and calls or open mail app.

Current code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    cell.lblEmail.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture =
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap)];
    [cell.lblEmail addGestureRecognizer:tapGesture];
    return cell;
}

- (void)labelTap:(UIGestureRecognizer *)sender {
    UILabel *labelSender = (UILabel*) sender;
    NSLog(@"%text: %@", labelSender.text);
}

ERROR:

2015-02-03 12:28:29.726 MyApp[80080:4019203] -[MyAppContactsViewController labelTap]: unrecognized selector sent to instance 0x7ff6d8cb4110
2015-02-03 12:28:29.743 MyApp[80080:4019203] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyAppContactsViewController labelTap]: unrecognized selector sent to instance 0x7ff6d8cb4110'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010709cf35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000106d35bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x00000001070a404d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x0000000106ffc27c ___forwarding___ + 988
    4   CoreFoundation                      0x0000000106ffbe18 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x00000001059912e6 _UIGestureRecognizerSendActions + 262
    6   UIKit                               0x000000010598ff89 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 532
    7   UIKit                               0x0000000105994ba6 ___UIGestureRecognizerUpdate_block_invoke662 + 51
    8   UIKit                               0x0000000105994aa2 _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 254
    9   UIKit                               0x000000010598ab1d _UIGestureRecognizerUpdate + 2796
    10  UIKit                               0x0000000105624ff6 -[UIWindow _sendGesturesForEvent:] + 1041
    11  UIKit                               0x0000000105625c23 -[UIWindow sendEvent:] + 667
    12  UIKit                               0x00000001055f29b1 -[UIApplication sendEvent:] + 246
    13  UIKit                               0x00000001055ffa7d _UIApplicationHandleEventFromQueueEvent + 17370
    14  UIKit                               0x00000001055db103 _UIApplicationHandleEventQueue + 1961
    15  CoreFoundation                      0x0000000106fd2551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    16  CoreFoundation                      0x0000000106fc841d __CFRunLoopDoSources0 + 269
    17  CoreFoundation                      0x0000000106fc7a54 __CFRunLoopRun + 868
    18  CoreFoundation                      0x0000000106fc7486 CFRunLoopRunSpecific + 470
    19  GraphicsServices                    0x0000000109fa59f0 GSEventRunModal + 161
    20  UIKit                               0x00000001055de420 UIApplicationMain + 1282
    21  MyApp                               0x00000001040769a3 main + 115
    22  libdyld.dylib                       0x0000000107a9c145 start + 1
    23  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Lücks
  • 3,806
  • 2
  • 40
  • 54
  • 1
    Why dont you just use UIButton with custom design? – Thallius Feb 03 '15 at 14:51
  • 1
    Why are you adding the gesture to the cell every time `tableView:cellForRowAtIndexPath:` is called? Every cell will end up with multiple gestures. This will eventually cause some sort of issue although I don't know what. Just add the gesture recogniser once. Or better yet, subclass `UITableViewCell` and add the gesture in IB. – Robotic Cat Feb 03 '15 at 15:21

2 Answers2

2

Change this line:

[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap)];

to

[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap:)];

This is required because labelTap as a selector is distinct from labelTap:. The former is a selector with no parameters (which doesn't exist, hence the exception), and the latter is the one with 1 parameter, which you have indeed defined.

Vinod Vishwanath
  • 5,821
  • 2
  • 26
  • 40
  • Wow, my bad! Worked now. Thanks. But I'm getting a new error (sabe selector sent to instance) on labelSender.text. There is a better way to do this? – Lücks Feb 03 '15 at 15:02
  • Like the comment on your original question suggests, a custom UIButton is a good option to consider unless it hinders you in some obvious way. UIButton being a native control also naturally takes care of a lot of gesture/scroll issues. I suspect your new error is because the `sender` wasn't the bottom itself, but some other view in the hierarchy. This would again never happen with a UIButton. – Vinod Vishwanath Feb 03 '15 at 15:08
0

Vinod, Claus were correct. I changed for UIButton and worked perfectly. I'm identifying witch mail was selected, with this code: Detecting which UIButton was pressed in a UITableView

Community
  • 1
  • 1
Lücks
  • 3,806
  • 2
  • 40
  • 54