UILabel in a NSMutableArray unrecognized selector - ios

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.

Related

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)

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!

NSMutabledictionary unrecognized selector sent to instance

I have this dictionary defined:
#property (strong,nonatomic,readwrite) NSMutableDictionary* images;
Along with this method:
-(void)initImages
{
UIImage *sprite = [UIImage imageNamed: #"sprite"];
[self.images
setObject:CFBridgingRelease(CGImageCreateWithImageInRect(sprite.CGImage, CGRectMake(0, 0, 64, 64)))
forKey:[NSNumber numberWithInt:black]];
}
And when I call:
[self.images objectForKey:[NSNumber numberWithInt:1]];
I get
2014-03-02 14:51:36.831 kljlkj[7853:70b] -[__NSCFType size]: unrecognized selector sent to instance 0x8b73ba0
Which happens on the line:
[self.images objectForKey:[NSNumber numberWithInt:1]];
If I skip the initImages function call, nothing happens and all is fine, however if I do call initImages I get the exception...
Why is this happening and how do I fix it?
EDIT: Sorry everybody! I was wrong, the exception was in fact thrown on another line than I thought and makes a lot of sense. Sorry again for the waste of time
This exception is means that the method you're calling doesn't exist on the object on which you called it. Somethjng is wrong with self.images when you make that call.

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.

Set the tltleColor of a button

My app is crashing when I try to change the color of the text which is inside a button myButton.titleLabel.textColor.
If I do that the app does not crash but the color of the text keeps in blackColor:
[myButton setTitleColor:[UIColor colorWithRed:0.41 green:0.107 blue:0.252 alpha:1.0] forState:UIControlStateNormal];
If I do this other way the app crashes:
.m:
-(IBAction)buttonTapped:(id)sender {
[myButton setTitleColor:myColor forState:UIControlStateNormal];
}
[...]
-(void) viewDidLoad {
myColor = [UIColor colorWithRed:0.41 green:0.107 blue:0.252 alpha:1.0];
}
.h:
UIColor *myColor;
debugger output:
2013-04-20 18:23:45.625 myApp[6291:c07] -[NSShadow set]: unrecognized selector sent to instance 0x753bfd0
2013-04-20 18:23:45.627 myApp[6291:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSShadow set]: unrecognized selector sent to instance 0x753bfd0'
*** First throw call stack:
(0x15bb012 0x12c8e7e 0x16464bd 0x15aabbc 0x15aa94e 0x369c87 0x369f76 0x368cd9 0x36b098 0x25ce6e 0x126a3f 0x12652c 0x1269ba 0x1262b6 0x126994 0x11b0e2 0x11b15c 0x990bc 0x9a227 0x9a8e2 0x1583afe 0x1583a3d 0x15617c2 0x1560f44 0x1560e1b 0x261c7e3 0x261c668 0x20cffc 0x21c2 0x20f5)
libc++abi.dylib: terminate called throwing an exception
(lldb)
How can I solve that? I want to use the second way (if possible).
The color object that you create with colorWithRed:green:blue:alpha: is autoreleased, so by the time buttonTapped: is called, the object has already been deallocated, so you have a pointer that points to garbage data (a dangling pointer).
You could either switch to using ARC (automatic reference counting), and/or create a retained property for myColor. That would look like this in your header:
#property (nonatomic, retain) UIColor *myColor;
Then, instead of setting the instance variable directly, use:
self.myColor = [UIColor colorWithRed:0.41 green:0.107 blue:0.252 alpha:1.0];
in viewDidLoad. If you don't use ARC, don't forget to release the color in dealloc:
- (void)dealloc
{
[_myColor release];
[super dealloc];
}

Resources