Reputation: 257
I'm fairly new to Objective C. I am trying to add a tap gesture recognizer to the UILabel. But the app crashes when trying to call the selector method. Here's what I am doing.
- (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
}
}
I followed this answer, but that is not helping me. https://stackoverflow.com/a/9058735/4863339
EDIT: Here is the crash report
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILabel onLabelTapped:]: unrecognized selector sent to instance 0x7f8c4f5252e0' *** First throw call stack: ( 0 CoreFoundation 0x000000011027334b exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010f7d821e objc_exception_throw + 48 2 CoreFoundation 0x00000001102e2f34 -[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: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: terminating with uncaught exception of type NSException
Upvotes: 1
Views: 1243
Reputation: 54
I guess you are passing the target wrong. just pass self instead of self.selectionHeaderLabel.
Then, code will be like this :
- (instancetype)initWithCard:(Card *)obj {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onLabelTapped:)];
_selectionHeaderLabel.userInteractionEnabled = YES;
[self.selectionHeaderLabel addGestureRecognizer:tap];
}
Hope this works for you.
Upvotes: 2