weird error when connecting in Interface Builder - iphone

i am getting a weird error when i connect my outlets in my xib.
I have a tab bar app, with 3 tabs. the first two work perfectly, but the third tab is having problems - it crashes the app when you push it when any IBOutlets are connected in IB. if you remove the connections, but leave them declared in xcode then it works fine. but as soon as i connect them again, it crashes the app.
any thoughts?
********** added code *********
.h
#interface Orders : UIViewController {
UILabel *username, *customer;
}
#property (nonatomic, readonly) IBOutlet UILabel *username, *customer;
.m
#implementation Orders #synthesize
username, customer;
message from console
*** Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[
setValue:forUndefinedKey:]: this class
is not key value coding-compliant for
the key username.'

Your posted code shows the UILabels are called customer and username but the screenshot shows them called customerLabel and usernameLabel in the nib file. Make sure that you're using the same names for the views everywhere (I recommend appending "Label" to the end of the names if they are UILabels. Better to have descriptive names.).
And like Kirby says in a comment to your question, make sure that the File's Owner in the nib file is set to be an instance of your UIViewController subclass (looks like it's called "Orders" but it should really be something like "OrdersViewController").

Related

ios StoryBoard - Subclass of UIImageView is showing up as a UIImageView rather than the subclass in IBOutlet variable

I have a subclass of UIImageView called SlideShowView. The SlideShowView .h and .m files are in a shared project that is referenced from my main project.
In my storyboard, I have an instance of SlideShowView connected to an IBOutlet in my ViewController.
When I try to call the SlideShowView method setImages: on that instance, I get this error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView setImages:]: unrecognized selector sent to instance 0x6b57360
When I do po [self slideShowView], which is defined as:
#property (weak, nonatomic) IBOutlet SlideShowView *slideshowView;, it shows up as <UIImageView: 0x6b57360; ....
So it seems that for some reason, it isn't being set as an instance of my subclass, but an instance of UIImageView.
UPDATE:
If I copy the SlideShowView.m and .h files out of the shared library to my main project, it works. So that makes me think it has something to do with storyboard not finding the shared library files.
UPDATE 2: I tried setting the customClass in the storyboard to fkdsfewnwef, and it didn't throw any error or anything, it just still shows up as a UIImageView. So now I'm pretty sure it has something to do with SlideShowView not being found in the linked static library project.
In the storyboard, select it and set it to be a SlideShowView in the Identity inspector. Otherwise, the storyboard cannot know what class it is intended to be.

removing a #property etc. generated by Storyboard and XCode

I'm trying to switch over to Storyboard from IB and code but got the following problem. Xcode created a #property with #synthesize and the viewDidUnLoad methods but I now don't need it and if I try to delete it, the code crashes. To make things worse, I in the meantime had connected it to a UIButton (I changed the class to allow this) by mistake. When it crashes, the code gives the message:
Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[
setValue:forUndefinedKey:]: this class is not key value
coding-compliant for the key lock.'
The question is the following. Where does XCode put the code for this and what do I have to remove to get back to where I started from?
Sounds like you have a connection set up in your nib that uses that property. If you delete the property, you need to update your nib to not try and set it.

Adding UINavigationBar to a UITabBarController

Ok so I actually got to add UINavigationBar inside a UITabBarController using the following tutorial: http://twilloapp.blogspot.com/2009/03/how-to-embed-navigation-controller.html
But now the issue is that whenever I add a new button inside one of the views it just crashes.
For example:
In the first view called FirstViewController I added:
IBOutlet UIButton *test;
Than I also created:
- (IBAction) doSomething:(id)sender;
I hook up the test button with the UI in interface builder. But when I run i get:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x3b12e80> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key test.'
Not sure what's going on with this.
Does anyone know of a solution?
Also, does anyone know where can I find good pre-built templates for these kinda apps so I can just start working rather than editing and setting things up.
Thanks
When you unarchive the nib file at runtime, it's going to invoke setTest:aButton on your controller, which will redirect to setValue:aButton forKey:#"test". Since it's throwing an exception that it doesn't know what the key #"test" means, chances are you forgot to put the #synthesize test; in your FirstViewController.m file.

Connecting a UILabel to an IBOutlet causes a crash

I have a UIViewController I am loading from a xib file and pushing onto the navigation controller stack.
In the header file for the View Controller, I have:
IBOutlet UILabel *myTitle;
I don't do anything with "myTitle" in the code yet; I was just setting up the connections.
When I compile and run the application, and there are no labels defined in the xib file (and thus, nothing attached to the IBOutlet), it works. The view controller animates into view just fine, showing the view I built in Interface builder.
If I add a label to the xib in the interface builder, but don't connect it to the outlet, and recompile, it still works, showing the label with the default text I entered for it.
But if I connect the IBOutlet myTitle to the label in interface builder, recompile, and run the app, it works fine until I try to push the view controller onto the navigation controller's stack, at which point I get a crash:
*** -[UILabel copyWithZone:]: unrecognized selector sent to instance 0x4558e20
If I disconnect the outlet again, it resumes working, showing the static label as before. It appears that there's something funky going on when the view gets displayed, because the crash happens when I push the view onto the navigation stack.
Am I not supposed to add an IBOutlet to a UILabel or something? Or is there something else going on? Any suggestions on where to look for trouble?
Yes, it could be caused by wrong param name, e.g. try this:
IBOutlet UILabel title;
and it'll call an exception like that:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UILabel copyWithZone:]: unrecognized selector sent to instance 0x143f520'
Did you create an accessor for your IBOutlet?
#property (assign) IBOutlet UILabel *myTitle;
And then, in your .m file
#synthesize myTitle
I had the same problem. Turns out *title is reserved and Xcode doesn't tell you this. Once I renamed it to something else it worked.
In my case I had a UILabel in a UIToolBar. What fixed it for me was to delete the toolBar and add a new one with a new UILabel. An IB bug.

iPhone app crashing with NSUnknownKeyException setValue:forUndefinedKey: [duplicate]

This question already has answers here:
Xcode - How to fix 'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X" error?
(79 answers)
Closed 7 years ago.
I'm writing my first iPhone app, so I haven't gotten around to figuring out much in the way of debugging.
Essentially my app displays an image and when touched plays a short sound.
When compiling and building the project in XCode, everything builds successfully, but when the app is run in the iPhone simulator, it crashes.
I get the following error:
Application Specific Information:
iPhone Simulator 1.0 (70), iPhone OS 2.0 (5A331)
*** Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<UIView 0x34efd0> setValue:forUndefinedKey:]: this class is not key value
coding-compliant for the key kramerImage.'
kramerImage here is the image I'm using for the background.
I'm not sure what NSUnknownKeyException means or why the class is not key value coding-compliant for the key.
(This isn't really iPhone specific - the same thing will happen in regular Cocoa).
NSUnknownKeyException is a common error when using Key-Value Coding to access a key that the object doesn't have.
The properties of most Cocoa objects can be accessing directly:
[#"hello world" length] // Objective-C 1.0
#"hello world".length // Objective-C 2.0
Or via Key-Value Coding:
[#"hello world" valueForKey:#"length"]
I would get an NSUnknownKeyException if I used the following line:
[#"hello world" valueForKey:#"purpleMonkeyDishwasher"]
because NSString does not have a property (key) called 'purpleMonkeyDishwasher'.
Something in your code is trying to set a value for the key 'kramerImage' on an UIView, which (apparently) doesn't support that key. If you're using Interface Builder, it might be something in your nib.
Find where 'kramerImage' is being used, and try to track it down from there.
Also when you rename a view, don't forget to delete the reference on File's Owner. It may also raise this error.
Here's one where you'll get this error - and how to fix it. I was getting it when loading a nib that just had a custom TableViewCell. I used IB to build a xib that just had the File's Owner, First Responder, and the TableViewCell. The TableViewCell just had 4 UILabels which matched up to a class with 4 IBOutlet UILabels called rootCell. I changed the class of the TableViewCell to be rootCell. It worked fine until a made a couple of changes and suddenly I was getting the setValue:forUndefinedKey: when I was just instantiating the class after loading it from a nib:
NSArray * nib = [[NSBundle mainBundle] loadNibNamed:#"rootCell-iPad" owner:self options:nil];
cell = [nib objectAtIndex:0];
It failed at the first line, when the nib was trying to load. After a while, I noticed that it was trying to match the IBOutlet Labels to the root controller, NOT the rootCell class! That was my tipoff. What I did wrong was to inadvertently change the File's Owner to rootCell class. When I changed it back to NSObject, then it didn't try to match up to the delegate (rootController) when loading. So, if you're doing the above, make File's Owner an NSObject, but make the UITableCell your desired class.
I had this situation and it turns out even after finding all instances of the variable and deleting them my app still crashed. Heres what happened... I created another instance of a variable from a textfield from my XIB into my viewController.h but I realized I didnt need it anymore and deleted it. It turns out my program saw that and kept trying to use it in the program so in the future if this happens to anywhere else just go into you XIB right-click on the button or textfield etc and delete any extra unused variables.
I had this same problem today. I didn't have the right class specified for the View Controller in my Navigation Controller. This will happen often if you fail to specify the correct class for your views in Interface Builder.
You'll also get invalid selector issues as well. Always check your Interface Builder classes and connections!
This is how i solved mine, in Interface builder right-click the View Controller, there should be an exclamation mark on the missing outlet or action method. Find and remove all the references and that solved it.
This happened because i deleted the action method in the .m file.
It seems you are doing
#interface MyFirstIphoneAppViewController : UIViewController<> {
UIImageView *InitialkramerImage;
}
#property(nonatomic,retain) IBOutlet UIImageView *InitialkramerImage;
Then after synthesizing that imageview, When you open "MyFirstIphoneAppViewController.xib" in Interface Builder, you are taking an Image View from Tool(menu)/Library den linking that outlet to the 'InitialkramerImage' from the Files Owner of "MyFirstIphoneAppViewController.xib". Then you saved the project. But after that you might change the name of the outlet variable "InitialkramerImage" to "kramerImage". So, after doing this
#interface MyFirstIphoneAppViewController : UIViewController<> {
UIImageView *kramerImage;
}
#property(nonatomic,retain) IBOutlet UIImageView *kramerImage;
and saving the project when you run it, There is no existance of the outlet of "InitialkramerImage" in the "MyFirstIphoneAppViewController.xib". So, when you run the project there will be no outlet referencing from Imageview to the 'kramerImage' and
"For displaying the view,
UIViewController will try to find the
outlet to "InitialkramerImage" which
is not existing."
So, It will throw the "NSUnknownKeyException".
You can check the missing outlet by
opening the nib(.xib) file then right
clicking on the 'Files Owner' of that.
If you have done this code somewhere else and had a zip/compressed file, try to extract it again. It may work I dont know why but I feel like it is an extraction issue.
or you can try to change the IBOutlet to kramerImage and bind it again in NIB.