2010-10-12 70 views
2

我有一個UITableView自定義單元格。每個單元格內有2個視圖。每個視圖都有一個UIGestureRecognizer,用於處理點擊事件。當點擊視圖時,我會向UINavigationController發送消息以推送詳細視圖。這種情況下工作正常,直到我實際上滾動表。表格滾動後,當用戶點擊單元格內的其中一個視圖時,應用程序崩潰。例如,我可以加載應用程序,點擊第二個單元格中的視圖,並將詳細視圖正確地推入屏幕。從那裏,我回到桌面,滾動到底部,回到頂部,並點擊相同的視圖。從那裏,應用程序崩潰與無法識別的選擇器錯誤。這是我設置的手勢(在viewDidLoad中):UITableView中的UIGestureRecognizer - 滾動後崩潰?

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; 
+0

什麼類是「自我」,它有一個handleTap實現? – willcodejavaforfood 2010-10-12 14:11:47

+0

self是ArticleTileViewController,它包含handleTap方法的實現 – 2010-10-12 14:15:24

+0

您的代碼僅用於設置識別器委託才能調用可見的視圖嗎? – willcodejavaforfood 2010-10-12 14:15:41

回答

10

基本上這問題意味着你將消息發送到一個對象,它不能識別該消息在所有的,即該方法是。缺少

你選擇創造是不正確應該是:

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

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

--UPDATE--

recognizer.delegate = self; 

我詢問道歉,但你肯定 '自我' 其實有方法handleTap:

+1

現在,當我點擊視圖時,我立即崩潰: - [__ NSCFType handleTap:]:無法識別的選擇器發送到實例0x6271170 2010-10-12 09:42:22.505 FLUD [5284:207] ***終止應用程序到期未捕獲的異常'NSInvalidArgumentException',原因:' - [__ NSCFType handleTap:]:無法識別的選擇發送到實例0x6271170' – 2010-10-12 13:45:13

+0

更新後,您的意見:) – willcodejavaforfood 2010-10-12 13:51:58

+0

該方法是在我的頭文件中聲明,我聲明類爲UIGestureRecognizerDelegate在接口聲明中。我錯過了什麼嗎? – 2010-10-12 14:04:20

2

我想出了我做錯了什麼。在我的初始化代碼中(來自另一個ViewController),我在將它們添加到UITableViewCell後發佈了ArticleTileViewControllers。這導致一個對象被消息不再在內存中。

愚蠢的程序員錯誤!

0

如果您不想實施委託方法,則不需要將委託設置爲自我。

UITapGestureRecognizer *touchGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userTouchedTheView:)]; 
    [touchGesture setCancelsTouchesInView:NO]; 
    [self.tableView addGestureRecognizer:touchGesture]; 

如果您只實現userTouchedTheView:方法也可以。