I have a label which stores any data that is entered. it has a property and has been synthesized.
#property (strong, nonatomic) IBOutlet UILabel *memoryDisplay;
#synthesize memoryDisplay;
-(void)viewDidLoad
{
[super viewDidLoad];
view.hidden = YES;
}
The label is in a view which is hidden on load
The view has a property and has been synthesized
on the same view there is a button for clearing a label
It has an Action
.h
- (IBAction)clearMemory:(id)sender;
And
.m
- (IBAction)clearMemory:(id)sender
{
self.memoryDisplay.text = #"";
}
However, when ever I try to run the app it crashes and gives me this error
* Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key clearMemory.'
* First throw call stack:
(0x13bc052 0x154dd0a 0x13bbf11 0x9b3032 0x924f7b 0x924eeb 0x93fd60 0x23291a 0x13bde1a 0x1327821 0x23146e 0xd8e2c 0xd93a9 0xd95cb 0x39a73 0x39ce2 0x39ea8 0x40d9a 0x11be6 0x128a6 0x21743 0x221f8 0x15aa9 0x12a6fa9 0x13901c5 0x12f5022 0x12f390a 0x12f2db4 0x12f2ccb 0x122a7 0x13a9b 0x1b28 0x1a85)
terminate called throwing an exceptionsharedlibrary apply-load-rules all
I also tried to add some if statements to check for possible problems:
- (IBAction)clearMemory:(id)sender
{
if (!view.hidden) {
if ([memoryDisplay.text length] > 1)
{
self.memoryDisplay.text = #"";
}
}
Can anyone tell me what the problem might be?
If I take off everything got to do with the clear button it all works perfectly.
Thanks :)
Check all IB outlet bindings for broken links
Check to see if your memoryDisplay property is connected from the viewController to the UILabel in the nib/xib.
I couldn't do it the way I wanted, however I did find a workaround.
I placed a hidden, uneditable text box in the view which was empty and set the labels text to the text box whenever the clear button was pressed.
Related
I created UILabel category which would do extra work when font property is changed.
I have chosen category over sublassing so I do not have to change class for all labels in my all XIB files. I just add this category declaration to the prefix header and category is visible in the whole project scope.
Implementation file:
//
// UILabel+XIBCustomFonts.m
#import "UILabel+XIBCustomFonts.h"
#implementation UILabel (XIBCustomFonts)
BOOL comes_from_nib = NO;
-(id)initWithCoder:(NSCoder *)aDecoder_{
self = [super initWithCoder:aDecoder_];
if (self) {
comes_from_nib = YES;
}
return self;
}
-(void)setFont:(UIFont *)font_{
[super setFont:font_];
NSLog(#"Setting from for label from XIB for name:%# size: %.1f - do font theme replacement if needed", self.font.fontName, self.font.pointSize);
}
#end
The surprising thing is the crash with following log:
-[UIButtonLabel setFont:]: unrecognized selector sent to instance 0x9ad1b70
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButtonLabel setFont:]: unrecognized selector sent to instance 0x9ad1b70'
Where did UIButtonLabel came form?
It happens even if I do extra Class checking in setFont: setter:
if ([self class] != [UILabel class] || !comes_from_nib) {
[super setFont:font_];
return;
}
Is there any way to override setFont: setter in UILabel without subclassing?
You cannot call super methods in categories! Never!
This is not possible when you use a category to augment a class' functionality, you are not extending the class, you are actually wholly overriding the existing method, you lose the original method completely. That's why your error appears.
So, I'm puzzled by a really weird crash occurring in my app.
Background: I have a class CustomButton overriding UIButton. Briefly, the interface looks like:
#interface CustomButton : UIButton
#property (nonatomic, getter = isLoading) BOOL loading;
#end
Implementation:
#implementation CustomButton
#synthesize loading = loading_;
- (id)init {
if ((self = [UIButton buttonWithType:UIButtonTypeCustom])) {
[self setTitle:NSLocalizedString(#"My Button", nil) forState:UIControlStateNormal];
[[self titleLabel] setShadowColor:[UIColor blackColor]];
[[self titleLabel] setShadowOffset:CGSizeMake(0, -1)];
[self setContentEdgeInsets:UIEdgeInsetsMake(0, 77, 0, 30)];
UIImage *backgroundImage = [[UIImage imageNamed:#"back.png"] stretchableImageWithLeftCapWidth:77 topCapHeight:0];
UIImage *highlightedBackgroundImage = [[UIImage imageNamed:#"back_highlighted.png"] stretchableImageWithLeftCapWidth:77 topCapHeight:0];
[self setBackgroundImage:backgroundImage forState:UIControlStateNormal];
[self setBackgroundImage:highlightedBackgroundImage forState:UIControlStateHighlighted];
[self sizeToFit];
}
return self;
}
- (void)setLoading:(BOOL)loading {
if (loading_ != loading) {
// Do fancy things
}
}
#end
Problem: I'm trying to use the button in a class, so I have property and so on:
...
#property(nonatomic, strong) CustomButton *customButton;
...
#syntesize customButton = customButton_;
...
- (void)aMethod {
self.customButton.loading = YES;
}
...
The button, of course, is initialized.
Then, upon executing the statement self.bookButton.loading = YES, I get this error
2012-08-28 12:54:25.091 MyApp[9465:15803] -[UIAccessibilityBundle
setLoading:]: unrecognized selector sent to instance 0x8650cc0
2012-08-28 12:54:25.091 MyApp[9465:15803] *** Terminating app due
to uncaught exception 'NSInvalidArgumentException', reason:
'-[UIAccessibilityBundle setLoading:]: unrecognized selector sent to
instance 0x8650cc0'
*** First throw call stack: (0x1ead022 0x203ecd6 0x1eaecbd 0x1e13ed0 0x1e13cb2 0x26bf5 0x257ce 0xfa938f 0xfa96a4 0xfa99a7 0xfb8aea
0x11600be 0x11603c7 0x1160436 0xf10e49 0xf10f34 0xc5bcaac 0xe04b54
0x272e509 0x1de4803 0x1de3d84 0x1de3c9b 0x27887d8 0x278888a 0xee0626
0x14cce 0x27b5 0x1) terminate called throwing an exception
I honestly have no idea about what to look for. It's either something really really stupid, or something really subdle.
I've been searching on google/stackoverflow already of course, but I didn't manage to get an answer.
Usually that error occurs, as far as I know, when the object doesn't "understand" the sent message, but this shouldn't be the case. I don't even get a warning from the compiler.
If you have even just an idea of what I could look for or you want more details, let me know, thanks.
EDIT: forgot to specify that I am using ARC.
UPDATE: In the answers below Phillip Mills said that it might have been a premature memory release issue. It's a good point, but Instrument didn't find any zombie. Moreover, I've put a log right before the crashy statement, where I try print information about the CustomButton object and few things related to a UIButton, like the title. Everything looks ok. The title is the one I've set, the frame is there and so on... so I believe the object (an actual UIButton) exists and it's also the expected one. The only thing is the "loading" property that messes everything up.
SOLUTION: the init method was the source of the problem. Instead of initializing an instance of the subclass of the button, it was initializing an instance of UIButton. So, conequentely, no "loading" properties could be found. Substituting init with initWithFrame and using the standard procedure to override the initializer, everything works great.
Still a mistery, though, the reason why I got UIAccessibilityBundle as the origin of the error and not a UIButton.
Are you casting anything to (CustomButton *) in your app? Double-check the casts - you might be incorrectly casting something that's not a CustomButton and storing the result in the customButton property.
The button is almost certainly being released prematurely and having its address re-used for a UIAccessibilityBundle object. Try turning on zombies (or run Instruments to look for zombies) and you should see better information if the memory management problem isn't obvious.
Try
#interface CustomButton : UIButton
#property (nonatomic, assign) BOOL loading;
#end
and
#implementation CustomButton
#synthesize loading = _loading;
- (id)init {
....
}
- (void)setLoading:(BOOL)loading {
if (self.loading != loading) {
// Do fancy things
}
}
#end
For all other instances of loading, use self.loading.
Without seeing the other parts of your code, this is my best guess.
I am facing this issue on device that assigning the selected row value of picker takes too much time to be shown on label while on simulator it works fine.
Below is the code:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
if (pickerView == rewardsName) {
NSString *selectedReward = [self.rewards objectAtIndex:row];
if ([reward1.text length]==0){
//set label 1
reward1.text = selectedReward;
}
if ([reward2.text length]==0){
//set label 2
reward2.text = selectedReward;
}
if([reward3.text length]==0){
//set label 3.
reward3.text = selectedReward;
}
}
else if(pickerView == selectNamazPicker){
aNumberString = [self.namazArray objectAtIndex:row];
NSLog(#"aNumberString:: %#", aNumberString);
}
}
Can anyone tell me what can be the issue ... I am unable to find one...
**
EDITED:
ISSUE RESOLVED:
**
For others who would face this issue.
I don't know whether it was creating the problem or not. But I did make these changes to my code.
In my .h file, previously I was just defining properties of UILabels (reward1, reward2 etc) with IBOutlet and not ivars. But then I defined ivars as IBOutlet UILabel reward1; etc and eliminated IBOutlet from properties then it started working fine.
Now the problem has resolved.
For others who would face this issue.
I don't know whether it was creating the problem or not. But I did make these changes to my code.
In my .h file, previously I was just defining properties of UILabels (reward1, reward2 etc) with IBOutlet and not ivars. But then I defined ivars as IBOutlet UILabel reward1; etc and eliminated IBOutlet from properties then it started working fine.
Now the problem has resolved.
First of all I have seen that there are many questions about "unrecognized selector sent to instance" issue.
I have seen few but saw nothing about accessing a defined in category property...
I have a category on UILabel with a property.
The getter and the setter are defined.
Actually I have the same property in 2 different categories (for 2 different classes: UIButton and UILabel).
The problem is that I can access this property for UIButton but not for UILabel.
Once I try to access any method/property in UILabel(text) category it drops the "-[UILabel test]: unrecognized selector sent to instance 0x4e539f0" exception.
Both categories files are imported.
I have no idea what is the problem.
Here is some code:
// UILabel+text.h
#interface UILabel (text)
- (void)test;
#end
// UILabel+text.m
#implementation UILabel (text)
- (void)test {
NSLog(#"test");
}
#end
// UIButton+text.h
#interface UIButton (text)
- (void)test;
#end
// UIButton+text.m
#implementation UIButton (text)
- (void)test {
NSLog(#"test");// works
}
#end
// Usage (in UIViewController class) - both elements are defined in XIB
[self.button test];// works
[self.label test];// exception
Any help will be appreciated.
I don't have a clue for possible problem...
Thank you.
Michael.
Are you using a static library? If so, add all_load to Other Linker Flags.
Are you sure "UILabel+text.m" is in the target?
I experienced the same issue in a project with multiple static frameworks.
Adding all_load to Linker Flags did not solve the issue for me!
I had to enable the Build Setting GENERATE_MASTER_OBJECT_FILE = YES so that the category was properly found during runtime.
I have a uitableview 'A' which has cells, on clicking one of the cell, it needs to push another table view 'B'. and now when a cell is clicked in 'B', its needs to open a simple view.
I have managed to display both the tables in two different views. but when a cell is clicked in the menu B. it hangs and shows the below message
> unrecognized selector sent to > instance
could someone please tell me how do i solve this.thanks
Please find below my code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger row = [indexPath row];
NSLog(#"entering the if loop libdecripviewcontoller");
if(self.libraryDescripViewController == nil){
NSLog(#"its creating new instance of libdecripviewcontoller");
LibraryDescripViewController *aLib = [[LibraryDescripViewController alloc] initWithNibName:#"LibraryDescripView" bundle:nil];
self.libraryDescripViewController = aLib;
[aLib release];
}
libraryDescripViewController.title = [NSString stringWithFormat:#"%#",[libraryMenu objectAtIndex:row]];
ULS1AppDelegate *delegate = [[UIApplication sharedApplication] delegate ];
[delegate.searchLibNavController pushViewController:libraryDescripViewController animated:YES];
}
Please find the output below
2010-06-27 20:13:15.521 ULS1[1020:207]
entering the if loop
libdecripviewcontoller 2010-06-27
20:13:15.533 ULS1[1020:207] its
creating new instance of
libdecripviewcontoller 2010-06-27
20:13:15.541 ULS1[1020:207] *
-[LibraryMenuTabViewController setLibraryDescripViewController:]:
unrecognized selector sent to instance
0x3c2ec70 2010-06-27 20:13:15.554
ULS1[1020:207] * Terminating app due
to uncaught exception
'NSInvalidArgumentException', reason:
'*** -[LibraryMenuTabViewController
setLibraryDescripViewController:]:
unrecognized selector sent to instance
0x3c2ec70' 2010-06-27 20:13:15.558
ULS1[1020:207] Stack: ( 29303899,
2512004361, 29685819, 29255286,
29107906, 14755, 3050054, 3033220,
287146, 29088448, 29084744, 37393941,
37394138, 2777007, 11184, 11038 )
You can get more information about where the error is happening by opening your debugger window, clicking on "Show Breakpoints", and setting the following two breakpoints:
[NSException raise]
objc_exception_throw
Now when your app is about to crash, the debugger will usually show you exactly what line of code is causing the problem.
For this reason, it's good practice to have these breakpoints on every app you are developing.
It seems that you are calling "self.libraryDescripViewController = aLib" which is another way of writing [self setLibraryDescripViewController:alib].
The most likely cause of this would be in that you haven't declared the method setLibraryDescripViewController in your class.
Hope this helps.
There is way too little information here to diagnose exactly whats causing this, but one thing is certain. The method you are sending to the object is not defined for that object. Please post the code thats causing the error and the .h and .m files for the simple view you are talking about.
You probably need to set the class of the view controller in IB to LibraryDescripViewController. Double click the LibraryDescripViewController.xib file in xcode and when IB opens, in the identity view of the file's owner set the class to LibraryDescripViewController