2017-02-24 20 views
1

私はObjective 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

EDIT:ここではキャッチされない例外により '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のCo reFoundation 0x00000001101fc494 CFRunLoopRunSpecific + 420 20 GraphicsServices GSEventRunModal + 161 21のUIKit 0x000000010d51cf34 UIApplicationMain + 159 22 0x0000000114460a6f CollPoll 0x000000010bdcf8df 111 23 +メインlibdyld.dylib + 1 を開始0x000000011267a68d) のlibC++ abi.dylib:のキャッチされない例外で終わりますタイプNSException

+0

クラッシュレポートを表示 –

+0

"initWithTarget:self.selectionHeaderLabel" – kb920

+0

@ kb920のように思われるかもしれませんが、クラッシュを防ぐためにはどうすればよいですか? –

答えて

1

私はあなたが間違ったターゲットを渡していると思います。 self.selectionHeaderLabelの代わりにselfを渡すだけです。

その後、コードは次のようになります。これはあなたのために働く

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

関連する問題