1322

I'm trying to link a UILabel with an IBOutlet created in my class.

My application is crashing with the following error"

*** 
Terminating app due to uncaught exception
'NSUnknownKeyException', reason: 
'[<UIViewController 0x6e36ae0> setValue:forUndefinedKey:]: 
this class is not key value coding-compliant for the key XXX.'

What does this mean & How can I fix it?

11
  • 15
    pedrotorres is right. Yes this is right. If you are doing a UITableViewCell, in IB remember to make teh File's Owner to NSObject, and the UITableViewCell'Class to the .h class you defined.
    – gdm
    May 24, 2013 at 7:56
  • 4
    When you encounter such an issue and the offending key is an action rather than an outlet then most probably you have an outlet which mistakenly references your action function name instead of your outlet variable name. Jun 4, 2015 at 15:50
  • 2
    You should notice that the name of the key in the error message (the OP is calling it 'XXXX') is the name you gave to something in your nib file. That's should help narrow down your search.
    – kris
    Jul 12, 2015 at 21:51
  • I filed rdar://22105925 asking Apple to make these errors more obvious at build time. :)
    – jtbandes
    Aug 1, 2015 at 22:35
  • 1
    how to actually IMMEDIATELY SOLVE the problem! stackoverflow.com/a/13812660/294884 fantastic tip
    – Fattie
    Sep 28, 2015 at 13:01

82 Answers 82

1 2
3
1

In my case, I forgot to input the Module of my ViewController inside Custom Class section (located in the right hand side of the storyboard screen)

0

Usually when this happens to me, @TechZen's answer does the trick. Yesterday, however, I spent an embarrassingly long time mucking with storyboard connections only to discover that the problem was in my code.

I have a custom view controller that handles various layouts in my storyboard, but one of the layouts needed a special label not used by the others. So I created a subclass like so:

@interface MyViewControllerSubclass : MyViewController

Then I added a private property in MyViewControllerSubclass.m:

@interface MyViewController ()
@property (weak, nonatomic) IBOutlet UILabel *crashesApp;
@end

Xcode happily allowed me to connect this IBOutlet, yet every time the view would load, the app would crash with the old "not key-value compliant for the key 'chrashesApp'".

The solution, which is semi-obvious in retrospect, was to change the private category to use the correct name, i.e., that of the subclass:

@interface MyViewControllerSubclass ()
@property (weak, nonatomic) IBOutlet UILabel *noMoreCrashing;
@end
0

This problem also happens if you want to design a small subview in a separate XIB file in Interface Builder, and in IB you set it to the same class as the parent view.

If you then show it like this:

UIViewController *vc = [[UIViewController alloc] initWithNibName:@"NameOfTheSubviewNibFile" bundle:nil];
[self.view addSubview:vc.view];

The view will appear, but if it's got IBOutlets connected to its File Owner, you'll get the error message. So, this should work instead:

  1. In your the parent view's code, declare an IBOutlet UIView *mySubview to reference the view in the subview's nib file
  2. In the subview's nib file, connect the File Owner to the view, and set it to mySubview
  3. show it by doing:
[[NSBundle mainBundle] loadNibNamed:@"NameOfTheSubviewNibFile" owner:self options:nil]
[self.view addSubview:mySubview];

and you will be fine!

0

I found another possibility that may cause the issue.

When using "initWithNibName" method with the wrong xib name, it will lead to this kind of crash too.

Such as you choose to change a xib file's name, and then foget to change the name used in "initWithNibName" method to the same name.

0

I remember having a similar problem in the past, I solved it by changing the line:

_vicMain = [[UIViewController alloc] initWithNibName:@"vicMainScreen" bundle:nil];

into:

#include "vicLogin_iPad.h"       // This is the H file of the class holding the code for
                                 // processing all the IBOUtlets for the Login screen
.
.
.

_vicMain = [[vicLogin_iPad alloc] initWithNibName:@"vicMainScreen" bundle:nil];

Notice that I was initially declaring UIViewController to init my _vicMain, after using a pop-up window on top, I realised that both are using the same UIViewController.

By:

1) INCLUDing your class (of the sub-view), along with the same module that is doing the above _vicMain (which is a view controller object/variable) i.e. it's "vicLogin_iPad.h" in my case , and:

2) Use the your custom constructor to declare the object (i.e. instead of "xxx = [UIViewController alloc] ... ", you use "xxx = [vicLogin_iPad alloc] ... " instead.

the problem is resolved.

I hope this helps as it was a pain to pinpoint with the lack of details from the error message...

Regards Heider Sati

0

I encounter the same error log when dealing with my tableview cell. I found that my UILabels have duplicated referencing outlets(you could check it out in the reference inspector) to both the file's owner and my cell's class. Things get well when I delete the reference to the file's owner.

0

I had the same error in yet another slightly different form:

In interface builder, I have a navigation controller with a custom subcontroller. The class name of this was set correctly, however the NIB name (select subcontroller, go to Attributes Inspector) was set to the wrong file (basically to one of a different target). Resetting this to the correct filename solved the issue.

0

I had this problem too. I had copied a view with an IBOutlet from one xib file to another xib file. And even though I had removed the old reference and create a new reference, this error was still occurring.

I ended up restarting xcode to fix this problem.

0

Another tricky case:

In IB added ViewController to storyboard, deleted its view and set Custom Class to "MyViewController" so view is instantiated from MyViewController.xib

Specifying Storyboard ID the same "MyViewController" causes exception.

Changing Storyboard ID to some different name resolves the issue.

0

Did you leave your picker object unconnected?

lol 54 answers and not one of them is my solution. Talk about a common error.

In my case it was because I had a picker object on my VC and had not set an outlet and action for it.

I often leave buttons etc unconnected when I am just looking to see how the layout looks. But it seems you cannot do this for a picker.

0

Just pay attention if you're trying to observe a value that doesn't exist.

This happened to me simply as I was observing the "Text" property of a text field when I was supposed to be observing the "text" property instead.

0

The cause of my trouble, was that I duplicated a storyboard file (outside of Xcode if I recall correctly), then all view controllers in the duplicated file had same object-ID as in the original file. The remedy is to copy-pasted the view controllers, and they will then get a new object-ID. You can see the object-ID in the Identity Inspector.

0

Make sure you add the custom class' (even empty) implementation in the .m file like:

@implementation MySubclass
@end
0

In my case, I had added a ViewController to the storyboard, but I didn't assign it a Storyboard ID in the designer. Once I gave it an ID, it worked.

using Xamarin/Visual Studio 2015.

0

Really stupid mistake with me. So I decided to share here, hope this will help another people like me.

I have another submodule in my project, and I name a new class with same name with another class in main module, my new class NOT have xib file(100% by code), but another class in main module have xib file. I think Xcode linked wrong the new class with the xib file in main module.

That the reason for crash with message:

this class is not key value coding-compliant for the key tableView.

0

I am using the Xamarin.IOS to create the custom nib. Finally I found the root cause, went to Xcode and selected the nib file, In the Interface Builder, choose the UIView class and select the Connections Inspector and remove the default outlets as shown below.enter image description here

0

I tried literally every option in this thread. The only thing that fixed it for me was changing the name of my custom view xib file.

My custom view and xib had the same name. Renaming the xib fixed it.

0

NSUnknownKeyException error

'NSUnknownKeyException', reason: this class is not key value coding-compliant for the key

I got this error when .xib file(e.g. TableViewCell.xib) had the same name as cell class(e.g. TableViewCell.swift)

The correct approach is:

TableViewCell.swift -> View.swift <-> View.xib
0
0

enter image description here

Remove button reference by clicking the button and then re-create reference to this button.

0

This thread is really an epic saga as @FranticRock mentioned.

In my case -

  1. the outlets are properly connected
  2. the "Inherit From Target" has a proper value which means the module is properly added

What caused the crash in my case is User Defined Runtime Attributes under the Identity Inspector

I have a view that somehow had three attributes borderColor, borderWidth and cornerRadius

enter image description here

Removing these attributes from that view resolved the issue.

0

As far as I'm concerned I stupidly changed the access modifier of my UIViewController to Private and of course it couldn't access it...

-1

In my case the View was UIView but the XIB Connection was to an IBOutlet of type LottieAnimationView. The actual lottie was a child of the UIView.

1 2
3

Not the answer you're looking for? Browse other questions tagged or ask your own question.