我试图创建一个表格视图,单元格包含姓名,电话和邮件。如果用户触摸电话,则应用程序呼叫电话(单击以呼叫)。如果用户触摸邮件,我需要使用新电子邮件打开邮件应用程序,然后点击“#34;触摸的地址”。
首先,我尝试开发包含电子邮件的可触摸标签,但我收到错误" 选择器发送到实例"。
我需要识别tapgesture,然后确定它是电子邮件还是电话,获取文本和电话或打开邮件应用程序。
当前代码:
- (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);
}
错误:
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
答案 0 :(得分:2)
更改此行:
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap)];
到
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap:)];
这是必需的,因为labelTap
selector
与labelTap:
不同。前者是一个没有参数的选择器(它不存在,因此是例外),而后者是带有1个参数的选择器,你确实已经定义了它。
答案 1 :(得分:0)
维诺德,克劳斯是对的。我换了UIButton,工作得很好。 我已选择使用此代码识别女巫邮件:Detecting which UIButton was pressed in a UITableView