UITableView中的UIGestureRecognizer - 滚动后崩溃?

时间:2010-10-12 09:28:11

标签: iphone uiview uigesturerecognizer

我有一个带自定义单元格的UITableView。每个单元格内部有2个视图。每个视图都附加了一个UIGestureRecognizer来处理点击事件。当点击视图时,我向UINavigationController发送消息以推送详细视图。这种情况正常,直到我实际滚动表。滚动表后,当用户点击单元格内的其中一个视图时,应用程序崩溃。例如,我可以加载应用程序,单击第二个单元格中的视图,然后将详细视图正确地推送到屏幕上。从那里,我导航回到桌面,滚动到底部,回到顶部,然后点击相同的视图。从那里,应用程序崩溃与无法识别的选择器错误。这是我的手势设置(在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]; 
    }

最后,这是我在控制台中获得的堆栈跟踪:

*** 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

编辑:经过一段时间的测试后,我确定只有当时屏幕上可见的视图才能触发事件。其他一切都因上述错误而崩溃。可能与this未答复的问题相关吗?

这是我的标题声明(为清晰起见,省略了iVars和属性:

@interface ArticleTileViewController : UIViewController<UIGestureRecognizerDelegate> {

}

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

3 个答案:

答案 0 :(得分:10)

基本上这个问题意味着您正在向一个根本无法识别该消息的对象发送消息,即该方法丢失。

您的选择器创建不正确应该是:

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

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

- UPDATE -

recognizer.delegate = self;

我为问道而道歉,但你确定“自我”实际上有方法handleTap:吗?

答案 1 :(得分:2)

我弄清楚我做错了什么。在我的初始化代码(来自另一个ViewController)中,我在将它们添加到UITableViewCell之后发布了ArticleTileViewControllers。这导致了一个不再在内存中的消息对象。

愚蠢的程序员错误!!

答案 2 :(得分:0)

如果您不想实现委托方法,则无需将委托设置为self。

    UITapGestureRecognizer *touchGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userTouchedTheView:)];
    [touchGesture setCancelsTouchesInView:NO];
    [self.tableView addGestureRecognizer:touchGesture];
如果你只实现userTouchedTheView:方法,那么

也可以。