UITableView 中的 UIGestureRecognizer - 滚动后崩溃? [英] UIGestureRecognizer in UITableView - Crash After Scrolling?
问题描述
我有一个带有自定义单元格的 UITableView.每个单元格内有 2 个视图.每个视图都有一个 UIGestureRecognizer 附加来处理点击事件.当视图被点击时,我向 UINavigationController 发送一条消息以推送详细信息视图.在我实际滚动表格之前,此场景工作正常.滚动表格后,当用户点击单元格内的视图之一时,应用程序会崩溃.例如,我可以加载应用程序,单击第二个单元格中的视图,然后将详细信息视图正确推送到屏幕上.从那里,我导航回表格,滚动到底部,回到顶部,然后点击相同的视图.从那里,应用程序因无法识别的选择器错误而崩溃.这是我的手势设置(在 viewDidLoad 中):
I have a UITableView with custom cells. Inside each cell is 2 views. Each view has a UIGestureRecognizer attached to handle tap events. When the view is tapped, I send a message to a UINavigationController to push a detail view. This scenario works fine until I actually scroll the table. After the table is scrolled, the app crashes when the user taps on one of the views inside of a cell. For example, I can load the app, click on a view in the 2nd cell, and get the detail view to be pushed onto the screen properly. From there, I navigate back to the table, scroll to the bottom, back to the top, and tap the same view. From there, the app crashes with an unrecognized selector error. Here is my setup for the gesture (in viewDidLoad):
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleTap:)];
[self.view addGestureRecognizer:recognizer];
recognizer.delegate = self;
[recognizer release];
这里是调用的方法:
-(void)handleTap:(UITapGestureRecognizer *)sender{
NSLog(@"Handling tap on ArticleTileViewController");
ArticleViewController *vc = [[ArticleViewController alloc] initWithArticleData:self.articleDataArray];
PROJECTAppDelegate *appDelegate = (PROJECTAppDelegate *)[UIApplication sharedApplication].delegate;
[appDelegate.navController pushViewController:vc animated:YES];
}[appDelegate.navController pushViewController:vc animated:YES];
}
最后,这是我在控制台中得到的堆栈跟踪:
Finally, here is the stack trace I get in the console:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType handleTap]: unrecognized selector sent to instance 0x607ba30'
*** Call stack at first throw:
(
0 CoreFoundation 0x02839b99 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x0298940e objc_exception_throw + 47
2 CoreFoundation 0x0283b6ab -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x027ab2b6 ___forwarding___ + 966
4 CoreFoundation 0x027aae72 _CF_forwarding_prep_0 + 50
5 UIKit 0x0057f060 -[UIGestureRecognizer _updateGestureWithEvent:] + 727
6 UIKit 0x0057b8bf -[UIGestureRecognizer _delayedUpdateGesture] + 47
7 UIKit 0x00580152 _UIGestureRecognizerUpdateObserver + 637
8 UIKit 0x00581464 _UIGestureRecognizerUpdateGesturesFromSendEvent + 51
9 UIKit 0x0032a844 -[UIWindow _sendGesturesForEvent:] + 1292
10 UIKit 0x003263bf -[UIWindow sendEvent:] + 105
11 UIKit 0x00309cb4 -[UIApplication sendEvent:] + 447
12 UIKit 0x0030e9bf _UIApplicationHandleEvent + 7672
13 GraphicsServices 0x02f07822 PurpleEventCallback + 1550
14 CoreFoundation 0x0281aff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
15 CoreFoundation 0x0277b807 __CFRunLoopDoSource1 + 215
16 CoreFoundation 0x02778a93 __CFRunLoopRun + 979
17 CoreFoundation 0x02778350 CFRunLoopRunSpecific + 208
18 CoreFoundation 0x02778271 CFRunLoopRunInMode + 97
19 GraphicsServices 0x02f0600c GSEventRunModal + 217
20 GraphicsServices 0x02f060d1 GSEventRun + 115
21 UIKit 0x00312af2 UIApplicationMain + 1160
22 PROJECT 0x00002459 main + 121
23 PROJECT 0x000023d5 start + 53
24 ??? 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException
'
经过一段时间的测试后,我确定只有当时在屏幕上可见的视图才会触发事件.其他一切都因上述错误而崩溃.可能与这个未回答的问题有关?
After testing with this for a while, I've determined that only the views that are visible on the screen at the time will fire the event. Everything else crashes with the above error. Possibly related to this unanswered question?
这是我的标头声明(为清楚起见,省略了 iVar 和属性:
Here is my header declaration (iVars and properties omitted for clarity:
@interface ArticleTileViewController : UIViewController<UIGestureRecognizerDelegate> {
}
-(void)handleTap:(UITapGestureRecognizer *)sender;
推荐答案
基本上这个问题意味着你正在向一个根本无法识别消息的对象发送消息,即缺少方法.
Basically this problems means that you are sending a message to an object that does not recognize the message at all, ie the method is missing.
您的选择器创建不正确,应该是:
Your selector creation is incorrect it should be:
UIGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
和
- (void) handleTap:(UITapGestureRecognizer *)sender {
--更新--
recognizer.delegate = self;
我很抱歉问这个问题,但你确定self"确实有方法 handleTap:
?
I apologize for asking, but are you sure that 'self' actually has the method handleTap:
?
这篇关于UITableView 中的 UIGestureRecognizer - 滚动后崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!