2017-02-24 31 views
1

後,我是相當新的目標C.我想敲擊手勢識別器添加到的UILabel。但是當試圖調用選擇器方法時,應用程序崩潰。這是我正在做的。應用程序崩潰的姿態加入到UITapGestureRecognizer的UILabel

 - (instancetype)initWithCard:(Card *)obj { 
//few lines of code here... 
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self.selectionHeaderLabel action:@selector(onLabelTapped:)]; 
        _selectionHeaderLabel.userInteractionEnabled = YES; 
        [self.selectionHeaderLabel addGestureRecognizer:tap]; 
    } 

    -(void) onLabelTapped:(UITapGestureRecognizer *) recognizer { 
     if ([recognizer state] == UIGestureRecognizerStateEnded) { 
      //do some stuff 
     } 
    } 

我跟着這個答案,但那不是幫助我。 https://stackoverflow.com/a/9058735/4863339

編輯:這裏是崩潰報告

終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因是: ' - [的UILabel onLabelTapped:]:無法識別的選擇發送到實例0x7f8c4f5252e0' * **第一擲調用堆棧: ( 0的CoreFoundation 0x000000011027334b exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010f7d821e objc_exception_throw + 48個 2的CoreFoundation 0X 00000001102e2f34 - [NSObject的(NSObject的)doesNotRecognizeSelector:] + 132 3的CoreFoundation 0x00000001101f8c15 ___forwarding_ + 1013 4的CoreFoundation 0x00000001101f8798 _CF_forwarding_prep_0 + 120 5的UIKit 0x000000010da5f289 - [UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] + 57 6的UIKit 0x000000010da67028 _UIGestureRecognizerSendTargetActions + 109 7的UIKit 0x000000010da64af7 _UIGestureRecognizerSendActions + 227 8的UIKit 0x000000010da63d83 - [UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 891 9的UIKit 0x000000010da4fe56 _UIGestureEnvironmentUpdate + 1395 10的UIKit 0x000000010da4f89b - [UIGestureEnvironment _deliverEvent:toGestureReco gnizers:usingBlock:] + 521 11的UIKit 0x000000010da4ea7e - [UIGestureEnvironment _updateGesturesForEvent:窗口:] + 286 12的UIKit 0x000000010d58d7ad - [一個UIWindow的SendEvent:] + 3989 13的UIKit 0x000000010d53aa33 - [UIApplication的的SendEvent:] + 371 14的UIKit 0x000000010dd2cb6d dispatchPreprocessedEventFromEventQueue + 3248 15的UIKit 0x000000010dd25817 __handleEventQueue + 4879 16的CoreFoundation 0x0000000110218311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 17的CoreFoundation 0x00000001101fd59c __CFRunLoopDoSources0 + 556 18的CoreFoundation 0x00000001101fca86 __CFRunLoopRun + 918 19共主義Refoundation 0x00000001101fc494 CFRunLoopRunSpecific + 420個 20個GraphicsServices 0x0000000114460a6f GSEventRunModal + 161 21的UIKit 0x000000010d51cf34 UIApplicationMain + 159 22 CollPoll 0x000000010bdcf8df主+ 111 23 libdyld.dylib 0x000000011267a68d啓動+ 1 ) 的libC++ abi.dylib:與未捕獲的異常終止鍵入NSException

+0

顯示崩潰報告 –

+0

也許這是因爲「initWithTarget:self.selectionHeaderLabel」 – kb920

+0

@ kb920 - 所以我應該怎麼做才能防止崩潰? –

回答

1

我猜你逝去的目標是錯誤的。只需傳遞self而不是self.selectionHeaderLabel。

然後,代碼將是這樣的:

- (instancetype)initWithCard:(Card *)obj { 

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onLabelTapped:)]; 
       _selectionHeaderLabel.userInteractionEnabled = YES; 
       [self.selectionHeaderLabel addGestureRecognizer:tap]; 
} 

希望這對你的作品。

+0

是的;這工作:)爲什麼我有添加initWithTarget作爲自己而不是我的標籤? –

+0

這是因爲需要調用的目標方法是在同一個類中。所以self是該類的引用。 – Appi