ios [__NSCFNumber length]: unrecognized selector sent to instance - ios

i have created one custom cell and put button on it
and button event is like this
[cell.BtnRequest addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
and didtapbutton code is this
- (void)didTapButton:(id)sender {
WorkerDetailsVC *wvc=[self.storyboard instantiateViewControllerWithIdentifier:#"workerdetailsvc"];
NSIndexPath *indepath = [self.tblorderdata indexPathForCell:sender];
wvc.arrworkarrequest=[arrData objectAtIndex:indepath.row];
NSLog(#" arr send :%#", wvc.arrworkarrequest);
wvc.struserid = struserid;
[self.navigationController pushViewController:wvc animated:YES];
}
and on the next page i got the array arrworkarrequest and after the load all next page it will show error like this
2014-10-01 15:08:42.607 demo[2151:60b] -[__NSCFNumber length]: unrecognized selector sent to instance 0x911a860
2014-10-01 15:08:42.613 dmeo[2151:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x911a860'

Your problem is that sender is of type UIButton not UITableViewCell so it's getting stuck in the [self.tblorderdata indexPathForCell:sender] because that method call expects a type of UITableViewCell.
You need to find the Cell that the button is in. You may be able to do this using a while loop (although it's a bit horrible). Something like:
id obj = sender;
while (![obj isKindOfClass:[UITableViewCell class]]) {
obj = [sender superview];
}
[self.tblorderdata indexPathForCell:obj];
This feels like it's abusing the view hierarchy though.

This happens if you compare the length of a string directly
for example
NSString *myString = #"2";
using this statement will generate the error
if(myString.length==2)

Related

UILabel in a NSMutableArray unrecognized selector

In my app, I have created a 'NSMutalbeArray', in which I add some 'UILabel'.
I correctly initialize my array :
_pickerMonthLabel = [[NSMutableArray alloc] init];
I correctly add UILabels in my array, but when I want to make some changes to a label, the behavior is strange. For example, I want to change the text color of a label in my array. I do :
[(UILabel *)[_pickerMonthLabel objectAtIndex:i] setTextColor:[UIColor redColor]];
This doesn't look like to work because my app crashes :
-[__NSCFConstantString setTextColor:]: unrecognized selector sent to instance 0x17e8d4
*** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSCFConstantString setTextColor:]: unrecognized selector sent to instance 0x17e8d4'
But i have no warning when I wrote this line. When I write another kind of line :
(UILabel *)[_pickerMonthLabel objectAtIndex:i].textcolor = [UIColor redColor];
It gives me an error :
Property 'textcolor' not found on object of type 'id'
I don't understand the difference between these two lines, and how to fix my problem...
Thank you in advance for your help.
I guess this is more of a remark, but you could try to use the respondsToSelector: message to check if the instance can handle the message you are trying to send. (This will make sure you don't get an exception) In your case this would be:
if ([[_pickerMonthLabel objectAtIndex:i] respondsToSelector:#selector(setTextColor:)]){
[(UILabel *)[_pickerMonthLabel objectAtIndex:i] setTextColor:[UIColor redColor]];
}
// add this to see what your class is
//else{
// NSLog(#"The class is: %#", [[_pickerMonthLabel objectAtIndex:i] class]);
//}
Hope it helps your debugging.

Casting tableViewCell as UIPickerView exactly how it is done with UIDatePicker for inline date picker not working

I am attempting to use an inline PickerView in my application, and it works fine except for one thing. when I try to set the row of the picker to a specific row based on a data argument my application crashes.
UITableViewCell *associatedTypePickerCell = [self.tableView cellForRowAtIndexPath:self.datePickerIndexPath];
UIPickerView *targetedPickerView = (UIPickerView *)[associatedTypePickerCell viewWithTag:kTypePickerTag];
if (targetedPickerView != nil){
NSLog(#"type picker ! = nil");
NSDictionary *itemData = self.dataArray[self.datePickerIndexPath.row-1];
Type *type = [itemData valueForKey:kDateKey];
NSInteger row = [[self types] indexOfObject:type];
[targetedPickerView selectRow:row inComponent:0 animated:NO];
}
I am getting this error:
-[UITableViewCell selectRow:inComponent:animated:]: unrecognized selector sent to instance 0x8ecba70
2014-07-22 23:47:23.384 MyGIJournal[16636:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell selectRow:inComponent:animated:]: unrecognized selector sent to instance 0x8ecba70'
does anyone know why this is crashing? it seems like casting like this to a UIPickerView isn't working, which doesn't make much sense cause this is the exact same way I do it for the UIDatePicker and it worked for that... Any help or advice would be greatly appreciated, Thanks!

How to update the selected property of a UIButton using the button's tag

I am trying to update the selected property for a UIButton by using the button’s tag. However, when I select the relevant view in the iOS simulator I get the following error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setSelected:]: unrecognized selector sent to instance 0x995b1e0'
After researching this issue and error, it appears that the UIView object doesn't know what to do with the setSelected method.
Here is my code where I am attempting to update the selected property:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"Documents folder: %#", [self documentsDirectory]);
NSLog(#"Data file path:%#", [self dataFilePath]);
UIButton *januarybtn = (UIButton *)[self.view viewWithTag:1];
[januarybtn setSelected:YES];
}
Also, here are several posts that I have tried to follow when making this update:
UIView viewWithTag 0 problem
Unrecognized selector sent to instance" problem
Thank you in advance for helping me.

Getting Error setting Title for the UIButton

Im attempting to make the title of a UIButton the favorites count of the particular tweet from twitter. I can functionally attain the number and I am successfully authorized with Twitter. Here is how I am attempting to set the Title:
//Set number of Favorites for Tweet
NSObject *favoritesCount = [[tweet objectForKey:#"user"]objectForKey:#"favourites_count"];
UIButton *favoritesButton = (UIButton *)[cell viewWithTag:204];
favoritesButton.titleLabel.text = favoritesCount;
When I run this I get the error at favoritesButton.titleLabel.text = favoritesCount;
Here is the error I am getting:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x8d3a3a0'
Title should be NSString value. You need to set title as NSString not NSObject.
NSInteger favoritesCount = [[tweet objectForKey:#"user"]objectForKey:#"favourites_count"];
UIButton *favoritesButton = (UIButton *)[cell viewWithTag:204];
favoritesButton.titleLabel.text = [NSString stringWithFormat:#"%d",favoritesCount];

App crashes when pushing a view controler

Hey when I push another view Controller i get this in my main.m
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
I am using this to push the view controller -
-(void)showMore:(UIButton *)sender
{
MoreViewController *moreViewController = [[MoreViewController alloc] init];
[self.navigationController pushViewController:moreViewController animated:YES];
}
I am sending the message here
[moreButton addTarget:self action:#selector(showSettings:) forControlEvents:UIControlEventTouchUpInside];
Here is my Error -
2013-09-25 18:16:03.186 Time Travel[1591:60b] Application windows are expected to have a root view controller at the end of application launch
2013-09-25 18:16:05.179 Time Travel[1591:60b] -[NSConcreteValue showSettings:]: unrecognized selector sent to instance 0x14e5ea70
2013-09-25 18:16:05.181 Time Travel[1591:60b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteValue showSettings:]: unrecognized selector sent to instance 0x14e5ea70'
* First throw call stack:
(0x2e1e5e8b 0x384e26c7 0x2e1e97b7 0x2e1e80b7 0x2e136e98 0x309a055f 0x309a04fb 0x309a04cb 0x3098c0f3 0x3099ff13 0x3099fbdd 0x3099ac09 0x3096ff59 0x3096e747 0x2e1b0f27 0x2e1b03ef 0x2e1aebdf 0x2e119541 0x2e119323 0x32e492eb 0x309d01e5 0x4cbd5 0x389dbab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
Does it crash on the first line or the second? Add a breakpoint to each and click the continue button to check which one.
If it's the former there might be some illegal code in your alloc/init for MoreViewController.
If it's the latter, maybe there some class/delegate methods (viewDidLoad, etc) are the culprit.
What is the error message during your crash? (Sometimes clicking the resume-play-button in the debugger in Xcode can reveal a bit more after a crash.)
I didnt find a method named showSettings: in the code you posted. You are pushing your viewController in method named showMore: So I think, the code should be like this:
[moreButton addTarget:self action:#selector(showMore:) forControlEvents:UIControlEventTouchUpInside];
Please check with this.
The error message saying showSettings: method is not found
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteValue **showSettings:**]: unrecognized selector sent to instance 0x14e5ea70'
And I've notified that you are using showMore: as the name of your method
-(void)showMore:(UIButton *)sender
maybe just change showMore to showSettings, or vice versa

Resources