2017-02-24 29 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