将手势UITapGestureRecognizer添加到UILabel

时间:2017-02-24 06:55:56

标签: ios objective-c uilabel uigesturerecognizer

我对Objective C很新。我正在尝试为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 0x00000001102e2f34 - [NSObject(NSObject)doesNotRecognizeSelector:] + 132       3 CoreFoundation 0x00000001101f8c15 ___转发_ + 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:toGestureRecognizers:usingBlock:] + 521       11 UIKit 0x000000010da4ea7e - [UIGestureEnvironment _updateGesturesForEvent:window:] + 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 CoreFoundation 0x00000001101fc494 CFRunLoopRunSpecific + 420       20 GraphicsServices 0x0000000114460a6f GSEventRunModal + 161       21 UIKit 0x000000010d51cf34 UIApplicationMain + 159       22 CollPoll 0x000000010bdcf8df main + 111       23 libdyld.dylib 0x000000011267a68d start + 1   )   libc ++ abi.dylib:以NSException类型的未捕获异常终止

1 个答案:

答案 0 :(得分:2)

我猜你错过了目标。只是通过self而不是self.selectionHeaderLabel。

然后,代码将是这样的:

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

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

希望这适合你。