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
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.
Related
I want to use sender to get the value from segmented control, but I am getting the unrecognized selector sent to instance error. The segmented control was added within a view. I've tried removing the semicolon and/or sender and none of the prior posts I've found seem to help. Any ideas?
Swift Code
var segCntrl = UISegmentedControl(items: ["Yes","No"])
segCntrl.tag = 100
segCntrl.center = CGPointMake(qView.bounds.width/2, qView.bounds.minY+40)
self.qView.addSubview(segCntrl)
func segmentorSwitch(sender: AnyObject) {
if(sender.selectedSegmentIndex == 1) {
println("Yes selected")
}
else if(sender.selectedSegmentIndex == 2) {
println("No selected")
}
}
segCntrl.addTarget(self, action: "segmentorSwitch:", forControlEvents: UIControlEvents.ValueChanged)
Error code:
2015-07-07 11:46:24.482 myApp [75641:1855377] -[[myApp.SegViewControllerr segmentorSwitch:]: unrecognized selector sent to instance 0x7fbb0a43b450
2015-07-07 11:46:24.489 myApp[75641:1855377] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[myApp.SegViewController segmentorSwitch:]: unrecognized selector sent to instance 0x7fbb0a43b450'
* First throw call stack:
(
0 CoreFoundation 0x00000001060dcc65 exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010838abb7 objc_exception_throw + 45
2 CoreFoundation 0x00000001060e40ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010603a13c ___forwarding_ + 988
4 CoreFoundation 0x0000000106039cd8 _CF_forwarding_prep_0 + 120
5 UIKit 0x00000001070bfd62 -[UIApplication sendAction:to:from:forEvent:] + 75
6 UIKit 0x00000001071d150a -[UIControl _sendActionsForEvents:withEvent:] + 467
7 UIKit 0x000000010724dfba -[UISegmentedControl _setSelectedSegmentIndex:notify:animate:] + 570
8 UIKit 0x000000010724ffbf -[UISegmentedControl touchesEnded:withEvent:] + 143
9 UIKit 0x000000010710c958 -[UIWindow _sendTouchesForEvent:] + 735
10 UIKit 0x000000010710d282 -[UIWindow sendEvent:] + 682
11 UIKit 0x00000001070d3541 -[UIApplication sendEvent:] + 246
12 UIKit 0x00000001070e0cdc _UIApplicationHandleEventFromQueueEvent + 18265
13 UIKit 0x00000001070bb59c _UIApplicationHandleEventQueue + 2066
14 CoreFoundation 0x0000000106010431 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17
15 CoreFoundation 0x00000001060062fd __CFRunLoopDoSources0 + 269
16 CoreFoundation 0x0000000106005934 __CFRunLoopRun + 868
17 CoreFoundation 0x0000000106005366 CFRunLoopRunSpecific + 470
18 GraphicsServices 0x000000010cc5ba3e GSEventRunModal + 161
19 UIKit 0x00000001070be8c0 UIApplicationMain + 1282
20 myApp 0x0000000104d9a0c7 main + 135
21 libdyld.dylib 0x0000000109458145 start + 1
22 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Looks like segmentorSwitch: is implemented inside another method. Define it outside the method in which your code is contained, and this should be fixed.
I'm trying to create a tableview that the cells contains name, telephone and mail. If the user touches the telephone, the app calls to the phone (click to call). If the user touches the mail, I need to open the mail app with a new email to the "touched address".
First, I'm trying to develop the touchable label that contains an email, but I'm receiving the error "selector sent to instance".
I need to identify the tapgesture, and then, identify if it's an email or telephone, get the text and calls or open mail app.
Current code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
cell.lblEmail.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(labelTap)];
[cell.lblEmail addGestureRecognizer:tapGesture];
return cell;
}
- (void)labelTap:(UIGestureRecognizer *)sender {
UILabel *labelSender = (UILabel*) sender;
NSLog(#"%text: %#", labelSender.text);
}
ERROR:
2015-02-03 12:28:29.726 MyApp[80080:4019203] -[MyAppContactsViewController labelTap]: unrecognized selector sent to instance 0x7ff6d8cb4110
2015-02-03 12:28:29.743 MyApp[80080:4019203] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyAppContactsViewController labelTap]: unrecognized selector sent to instance 0x7ff6d8cb4110'
*** First throw call stack:
(
0 CoreFoundation 0x000000010709cf35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000106d35bb7 objc_exception_throw + 45
2 CoreFoundation 0x00000001070a404d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000106ffc27c ___forwarding___ + 988
4 CoreFoundation 0x0000000106ffbe18 _CF_forwarding_prep_0 + 120
5 UIKit 0x00000001059912e6 _UIGestureRecognizerSendActions + 262
6 UIKit 0x000000010598ff89 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 532
7 UIKit 0x0000000105994ba6 ___UIGestureRecognizerUpdate_block_invoke662 + 51
8 UIKit 0x0000000105994aa2 _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 254
9 UIKit 0x000000010598ab1d _UIGestureRecognizerUpdate + 2796
10 UIKit 0x0000000105624ff6 -[UIWindow _sendGesturesForEvent:] + 1041
11 UIKit 0x0000000105625c23 -[UIWindow sendEvent:] + 667
12 UIKit 0x00000001055f29b1 -[UIApplication sendEvent:] + 246
13 UIKit 0x00000001055ffa7d _UIApplicationHandleEventFromQueueEvent + 17370
14 UIKit 0x00000001055db103 _UIApplicationHandleEventQueue + 1961
15 CoreFoundation 0x0000000106fd2551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
16 CoreFoundation 0x0000000106fc841d __CFRunLoopDoSources0 + 269
17 CoreFoundation 0x0000000106fc7a54 __CFRunLoopRun + 868
18 CoreFoundation 0x0000000106fc7486 CFRunLoopRunSpecific + 470
19 GraphicsServices 0x0000000109fa59f0 GSEventRunModal + 161
20 UIKit 0x00000001055de420 UIApplicationMain + 1282
21 MyApp 0x00000001040769a3 main + 115
22 libdyld.dylib 0x0000000107a9c145 start + 1
23 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Change this line:
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(labelTap)];
to
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(labelTap:)];
This is required because labelTap as a selector is distinct from labelTap:. The former is a selector with no parameters (which doesn't exist, hence the exception), and the latter is the one with 1 parameter, which you have indeed defined.
Vinod, Claus were correct. I changed for UIButton and worked perfectly.
I'm identifying witch mail was selected, with this code: Detecting which UIButton was pressed in a UITableView
When i press my button, i got a error of access.
the error : Thread 1 : EXC_BAD_ACCESS(code=1,address=0xd0a937db)
TTest.h
#interface TTtest : NSObject
{
UIButton *monBouton ;
UIImage *skin;
}
- (void)initTest :(UIView*)vueDonne;
-(void)test:(id)sender;
TTest.m
- (void)initTest :(UIView*)vueDonne
{
skin = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"boutonG" ofType:#"png"]];
monBouton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
monBouton.frame = CGRectMake(50, 50, 45, 50);
[monBouton setImage:skin forState:UIControlStateNormal];
[monBouton addTarget:self action:#selector(test:) forControlEvents:UIControlEventTouchDown];
[vueDonne addSubview: monBouton];
}
-(void)test:(id)sender //didn't work because i have the probleme
{
NSLog(#"test clicked");
}
testViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
TTtest *test =[[TTtest alloc] init];
[test initTest:_testView]; //View of my application
}
EDIT:
If i add monBouton = [[UIButton alloc] init];
I get a problem of SIGABRT
2014-10-26 16:47:22.827 testAsk[2134:a0b] -[CALayerArray test:]: unrecognized selector sent to instance 0xa141ea0
2014-10-26 16:47:22.831 testAsk[2134:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CALayerArray test:]: unrecognized selector sent to instance 0xa141ea0'
*** First throw call stack:
(
0 CoreFoundation 0x017395e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x014bc8b6 objc_exception_throw + 44
2 CoreFoundation 0x017d6903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x0172990b ___forwarding___ + 1019
4 CoreFoundation 0x017294ee _CF_forwarding_prep_0 + 14
5 libobjc.A.dylib 0x014ce874 -[NSObject performSelector:withObject:withObject:] + 77
6 UIKit 0x0022c0c2 -[UIApplication sendAction:to:from:forEvent:] + 108
7 UIKit 0x0022c04e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
8 UIKit 0x003240c1 -[UIControl sendAction:to:forEvent:] + 66
9 UIKit 0x00324484 -[UIControl _sendActionsForEvents:withEvent:] + 577
10 UIKit 0x003231fd -[UIControl touchesBegan:withEvent:] + 254
11 UIKit 0x0026934b -[UIWindow _sendTouchesForEvent:] + 386
12 UIKit 0x0026a184 -[UIWindow sendEvent:] + 1232
13 UIKit 0x0023de86 -[UIApplication sendEvent:] + 242
14 UIKit 0x0022818f _UIApplicationHandleEventQueue + 11421
15 CoreFoundation 0x016c283f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
16 CoreFoundation 0x016c21cb __CFRunLoopDoSources0 + 235
17 CoreFoundation 0x016df29e __CFRunLoopRun + 910
18 CoreFoundation 0x016deac3 CFRunLoopRunSpecific + 467
19 CoreFoundation 0x016de8db CFRunLoopRunInMode + 123
20 GraphicsServices 0x0368e9e2 GSEventRunModal + 192
21 GraphicsServices 0x0368e809 GSEventRun + 104
22 UIKit 0x0022ad3b UIApplicationMain + 1225
23 testAsk 0x000027bd main + 141
24 libdyld.dylib 0x01d75725 start + 0
25 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
The reason for the crash is that test object is dealloced when it receives the button action.
TTtest *test =[[TTtest alloc] init]; //dealloced after viewDidLoad
so try to make the test a property and use self.test.
self.test =[[TTtest alloc] init];
Doesn't look like you defined the selector "test"
-(void)test: (id) sender
NSLog(#"test clicked");
}
I used once a Swipe gesture on xCode 6 (using Swift). It worked well.
#IBAction func ArrasteiDireita(sender: UISwipeGestureRecognizer) {
println("Arrastei Direita")
}
I removed the gesture (in the main.storyboard) and trying to use Pan Gesture instead, but I don't know why the Swipe Gesture is still being required.
I am getting the following error if the associated method (ArrasteiDireita above) is commented:
2014-07-05 10:48:51.400 Verdadeiro ou Falso[51178:2926619] - [_TtC19Verdadeiro_ou_Falso4Jogo ArrasteiDireita:]: unrecognized selector sent to instance 0xb23bfa0
2014-07-05 10:48:51.402 Verdadeiro ou Falso[51178:2926619] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_TtC19Verdadeiro_ou_Falso4Jogo ArrasteiDireita:]: unrecognized selector sent to instance 0xb23bfa0'
*** First throw call stack:
(
0 CoreFoundation 0x00842646 __exceptionPreprocess + 182
1 libobjc.A.dylib 0x0216d8e3 objc_exception_throw + 44
2 CoreFoundation 0x00849575 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3 CoreFoundation 0x00794407 ___forwarding___ + 1047
4 CoreFoundation 0x00793fce _CF_forwarding_prep_0 + 14
5 UIKit 0x01466e63 _UIGestureRecognizerSendActions + 327
6 UIKit 0x0146509c -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 1657
7 UIKit 0x0146772d -[UIGestureRecognizer _delayedUpdateGesture] + 60
8 UIKit 0x0146b1cc ___UIGestureRecognizerUpdate_block_invoke689 + 347
9 UIKit 0x0146af6d _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 317
10 UIKit 0x0145e0a8 _UIGestureRecognizerUpdate + 3786
11 UIKit 0x0108ff3b -[UIWindow _sendGesturesForEvent:] + 1356
12 UIKit 0x01090d9f -[UIWindow sendEvent:] + 769
13 UIKit 0x01056c29 -[UIApplication sendEvent:] + 242
14 UIKit 0x010665e2 _UIApplicationHandleEventFromQueueEvent + 20518
15 UIKit 0x0103bcc5 _UIApplicationHandleEventQueue + 2154
16 CoreFoundation 0x00766faf __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
17 CoreFoundation 0x0075c34d __CFRunLoopDoSources0 + 253
18 CoreFoundation 0x0075b8a8 __CFRunLoopRun + 952
19 CoreFoundation 0x0075b22b CFRunLoopRunSpecific + 443
20 CoreFoundation 0x0075b05b CFRunLoopRunInMode + 123
21 GraphicsServices 0x0417e3ca GSEventRunModal + 192
22 GraphicsServices 0x0417e207 GSEventRun + 104
23 UIKit 0x0103f6e5 UIApplicationMain + 1526
24 Verdadeiro ou Falso 0x0000d591 top_level_code + 97
25 Verdadeiro ou Falso 0x0000d5cb main + 43
26 libdyld.dylib 0x026aeac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Since Swipe gesture occurs before the Pan Gesture, I am not being able to reach the Pan Gesture method:
#IBAction func Arrastei(sender: UIPanGestureRecognizer) {
println("Arrastado")
}
Is there any other place I need to remove the reference to Swipe Gesture?
I have a lot of textfields and what I am trying to make them do is dismiss when the return key is used. In order for this to work you need to set the delegate of each textfield to self, like this [textfield setDelegate: self];. I have over 50 textfields in my project and in order to make it so they all dismiss I have to copy that line of code for each textfield. In the example below I used a for loop to try to shrink this down, but my project crashes and gives me this error when I try. Can someone tell me what im doing wrong and how i can fix this?
//.h
#interface InsertScheduleCGPS : UIViewController <UITextFieldDelegate>{
NSArray *Dayh;
IBOutlet UITextField *Day11;
}
#property(nonatomic, assign) id<UITextFieldDelegate> delegate;
#property (nonatomic,strong) NSArray *Dayh;
.
//.m
- (void)viewDidLoad
{
[super viewDidLoad];
Dayh = [NSArray arrayWithObjects:#"Day11", nil];
NSLog(#"euf");
for(int i=0; i<[self.Dayh count]; i++) {
NSLog(#"dd%#",[self.Dayh objectAtIndex:i]);
[[self.Dayh objectAtIndex:i] setDelegate: self];
}
- (BOOL)textFieldShouldReturn:(UITextField *)Day11 {
[[self view] endEditing:YES];
return NO;
}
ERROR:
2014-01-18 19:15:26.712 Swepple[64912:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString setDelegate:]: unrecognized selector sent to instance 0x144ec'
*** First throw call stack:
(
0 CoreFoundation 0x0183b5e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x015be8b6 objc_exception_throw + 44
2 CoreFoundation 0x018d8903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x0182b90b ___forwarding___ + 1019
4 CoreFoundation 0x0182b4ee _CF_forwarding_prep_0 + 14
5 Swepple 0x0000bbd6 -[InsertScheduleCGPS viewDidLoad] + 4262
6 UIKit 0x00440318 -[UIViewController loadViewIfRequired] + 696
7 UIKit 0x004405b4 -[UIViewController view] + 35
8 UIKit 0x0044f361 -[UIViewController viewControllerForRotation] + 63
9 UIKit 0x00446f00 -[UIViewController _visibleView] + 84
10 UIKit 0x006d511a -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:animation:] + 5199
11 UIKit 0x0044c0fc -[UIViewController presentViewController:withTransition:completion:] + 6433
12 UIKit 0x0044c61f -[UIViewController presentViewController:animated:completion:] + 130
13 UIKit 0x0044c65f -[UIViewController presentModalViewController:animated:] + 56
14 UIKit 0x00870e16 -[UIStoryboardModalSegue perform] + 271
15 UIKit 0x0086107e -[UIStoryboardSegueTemplate _perform:] + 174
16 UIKit 0x00442280 -[UIViewController performSegueWithIdentifier:sender:] + 72
17 Swepple 0x000052d4 -[SecondViewController insert:] + 244
18 libobjc.A.dylib 0x015d0874 -[NSObject performSelector:withObject:withObject:] + 77
19 UIKit 0x0032e0c2 -[UIApplication sendAction:to:from:forEvent:] + 108
20 UIKit 0x0032e04e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
21 UIKit 0x004260c1 -[UIControl sendAction:to:forEvent:] + 66
22 UIKit 0x00426484 -[UIControl _sendActionsForEvents:withEvent:] + 577
23 UIKit 0x00425733 -[UIControl touchesEnded:withEvent:] + 641
24 UIKit 0x0036b51d -[UIWindow _sendTouchesForEvent:] + 852
25 UIKit 0x0036c184 -[UIWindow sendEvent:] + 1232
26 UIKit 0x0033fe86 -[UIApplication sendEvent:] + 242
27 UIKit 0x0032a18f _UIApplicationHandleEventQueue + 11421
28 CoreFoundation 0x017c483f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
29 CoreFoundation 0x017c41cb __CFRunLoopDoSources0 + 235
30 CoreFoundation 0x017e129e __CFRunLoopRun + 910
31 CoreFoundation 0x017e0ac3 CFRunLoopRunSpecific + 467
32 CoreFoundation 0x017e08db CFRunLoopRunInMode + 123
33 GraphicsServices 0x037e09e2 GSEventRunModal + 192
34 GraphicsServices 0x037e0809 GSEventRun + 104
35 UIKit 0x0032cd3b UIApplicationMain + 1225
36 Swepple 0x0000e04d main + 141
37 libdyld.dylib 0x01d7c70d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
You probably meant
Dayh = [NSArray arrayWithObjects:Day11, nil];
At present, Dayh is an array containing the string "Day11", not the text field.