Getting Error setting Title for the UIButton - ios

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];

Related

How can I change the image displayed in an UIImageView in a cell programmatically?

Hi Guys i was wondering how to set the image of an UIimageView that is in a cell. I'm rather new to coding so please fully explain if u can. I currently have this:
cell.image.image = [tempObject objectForKey:#"CafeImage"];
But the app crashes with this everytime:
-[PFFile imageAsset]: unrecognized selector sent to instance 0x7a96cd80 2015-02-22 15:06:19.616 Cafe[12830:140389] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PFFile imageAsset]: unrecognized selector sent to instance 0x7a96cd80'
It should be cell.imageView.image=[tempObject objectForKey:#"CafeImage"];
first - If you are using a standard cell, you should set
cell.imageView.image = "UIImage object";
second - Data in [tempObject objectForKey:#"CafeImage"] must be kind of UIImage class, or if it's NSString -> use [UIImage imageName:#"for example - objectForKey"];

I get this error in my code 'NSInvalidArgumentException',

2015-02-03 22:44:17.468 descuentos[1430:55158] -[UIButton value]: unrecognized selector sent to instance 0x7fde78d95440
2015-02-03 22:44:17.472 descuentos[1430:55158] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton value]: unrecognized selector sent to instance 0x7fde78d95440'
*** First throw call stack:
How can I fix this error? I think this is a thread error.
Here is my code:
- (IBAction)TipsSlider:(id)sender {
UISlider *slider = sender;
float valorFloat = slider.value;
int valInt = (int)valorFloat;
_dataSlider.text = [[NSString alloc]initWithFormat:#"%d", valInt];
}
- (IBAction)calcular:(id)sender {
UISlider *slider = sender;
float valorFloat = slider.value;
int valInt = (int)valorFloat;
_dataSlider.text = [[NSString alloc]initWithFormat:#"%d", valInt];
float valorIn= [[entrada text] floatValue];
float resultado = (valorFloat / 100) * valorIn;
NSString *resultadoFinal = [[NSString alloc]initWithFormat:#"%4.2f",resultado];
_salidaResultado.text = resultadoFinal;
}
It looks like you have connected an IBAction from a UIButton when you intended to use a UISlider.
You then assign the sender of the action (which is a button) to a slider and try to access its "value", only that UIButton instances do not respond to a "value" message and that's why you get the crash.
So, make sure you are "ctrl+dragging" from a slider in IB.

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.

ios [__NSCFNumber length]: unrecognized selector sent to instance

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)

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.

Resources