TableViewCell with touchable's labels - ios

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

Related

App crashes after adding gesture UITapGestureRecognizer to UILabel

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.

segmented control - unrecognized selector sent to instance

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.

Error EXC_BAD_ACCESS when pressing button

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");
}

How to add UIRefreshControl

I want to add UIRefreshControl to my app which fetches the records from the database when the table is pulled down , I have done the following code but it is giving me error like
2014-08-06 12:31:56.263 QuizUsindDB[1183:60b] -[ScoreViewController readScoreFromDatabase]: unrecognized selector sent to instance 0x8d3d8e0
2014-08-06 12:31:56.266 QuizUsindDB[1183:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ScoreViewController readScoreFromDatabase]: unrecognized selector sent to instance 0x8d3d8e0'
*** First throw call stack:
(
0 CoreFoundation 0x018f81e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x016778e5 objc_exception_throw + 44
2 CoreFoundation 0x01995243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x018e850b ___forwarding___ + 1019
4 CoreFoundation 0x018e80ee _CF_forwarding_prep_0 + 14
5 libobjc.A.dylib 0x0168982b -[NSObject performSelector:withObject:] + 70
6 UIKit 0x003393b9 -[UIApplication sendAction:to:from:forEvent:] + 108
7 UIKit 0x00339345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
8 UIKit 0x0043abd1 -[UIControl sendAction:to:forEvent:] + 66
9 UIKit 0x0043afc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
10 UIKit 0x0043ac06 -[UIControl sendActionsForControlEvents:] + 48
11 UIKit 0x00a227e9 -[UIRefreshControl _setRefreshControlState:notify:] + 404
12 UIKit 0x00a22650 -[UIRefreshControl setRefreshControlState:] + 48
21 CoreFoundation 0x0195d43e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14
22 CoreFoundation 0x0189e5cb __CFRunLoopRun + 1963
23 CoreFoundation 0x0189d9d3 CFRunLoopRunSpecific + 467
24 CoreFoundation 0x0189d7eb CFRunLoopRunInMode + 123
25 GraphicsServices 0x038ec5ee GSEventRunModal + 192
26 GraphicsServices 0x038ec42b GSEventRun + 104
27 UIKit 0x00337f9b UIApplicationMain + 1225
28 QuizUsindDB 0x0001038d main + 141
29 libdyld.dylib 0x01e41701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Code for UIRefreshControl is given above:
-(void)pullToRefresh
{
self.refreshControl = [[UIRefreshControl alloc] init];
self.refreshControl.tintColor = [UIColor blackColor];
[self.refreshControl addTarget:self action:#selector(readScoreFromDatabase)
forControlEvents:UIControlEventValueChanged];
[self.myTable addSubview:self.refreshControl];
[self.refreshControl endRefreshing];
}
I have called [self pullToRefresh] in viewDidLoad .
I have the above code for initializing and calling a selector to refresh the table.
I am not able to recognise the error, can anyone point me out mistake.
Change this line:
[self.refreshControl endRefreshing];
To this:
[self.refreshControl beginRefreshing];
The endRefreshing method should be called after your table view has finished reloading data where necessary.
Also, the crash log indicates that your -[ScoreViewController readScoreFromDatabase]: function doesn't exist.

NSURL unrecognized selector sent to instance

Here is the line of code
cell.imageURL=[NSURL URLWithString:self.imageURLs[indexPath.row]];
Here is the error trace
[UITableViewCell setImageURL:]: unrecognized selector sent to instance 0x906da70
2014-07-24 17:16:50.049 MyApp_iOS[9443:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell setImageURL:]: unrecognized selector sent to instance 0x906da70'
*** First throw call stack:
(
0 CoreFoundation 0x01a8c1e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x016ad8e5 objc_exception_throw + 44
2 CoreFoundation 0x01b29243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x01a7c50b ___forwarding___ + 1019
4 CoreFoundation 0x01a7c0ee _CF_forwarding_prep_0 + 14
5 MyApp_iOS 0x0007f518 -[BCDDogViewController tableView:cellForRowAtIndexPath:] + 504
6 UIKit 0x0046611f -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 412
7 UIKit 0x004661f3 -[UITableView _createPreparedCellForGlobalRow:] + 69
8 UIKit 0x00447ece -[UITableView _updateVisibleCellsNow:] + 2428
9 UIKit 0x0045c6a5 -[UITableView layoutSubviews] + 213
10 UIKit 0x003dc964 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
11 libobjc.A.dylib 0x016bf82b -[NSObject performSelector:withObject:] + 70
12 QuartzCore 0x03ef745a -[CALayer layoutSublayers] + 148
13 QuartzCore 0x03eeb244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
14 QuartzCore 0x03eeb0b0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
15 QuartzCore 0x03e517fa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
16 QuartzCore 0x03e52b85 _ZN2CA11Transaction6commitEv + 393
17 QuartzCore 0x03e53258 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
18 CoreFoundation 0x01a5436e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
19 CoreFoundation 0x01a542bf __CFRunLoopDoObservers + 399
20 CoreFoundation 0x01a32254 __CFRunLoopRun + 1076
21 CoreFoundation 0x01a319d3 CFRunLoopRunSpecific + 467
22 CoreFoundation 0x01a317eb CFRunLoopRunInMode + 123
23 GraphicsServices 0x03a805ee GSEventRunModal + 192
24 GraphicsServices 0x03a8042b GSEventRun + 104
25 UIKit 0x0036df9b UIApplicationMain + 1225
26 MyApp_iOS 0x000577c2 main + 130
27 libdyld.dylib 0x0216d701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
And the property in question is
#property(strong,nonatomic) NSURL *imageURL;
with setter
-(void)setImageURL:(NSURL *)imageURL
{
_imageURL=imageURL;
[self startDownloadingImage];
}
The strange thing is this used to work and about an hour or so ago it stops working and starts throwing this exception.
update
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BCDDogImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[BCDDogImageTableViewCell cellPrototypeIdentifier] forIndexPath:indexPath];
NSLog(#"THE CELL IS: %# ", cell);
cell.imageURL=[NSURL URLWithString:self.imageURLs[indexPath.row]];
return cell;
}
The error is because your cell is actually a UITableViewCell instance and not an instance of your custom cell class.
Be sure to register your custom cell class for the cell.

Resources