68

I know the meaning of this error, but I'm really struggling with it, and I need someone's help :

2010-09-21 15:03:11.562 Stocks[5605:207] *** Terminating app due to uncaught 
exception 'NSUnknownKeyException', reason: '[<NSObject 0x499fb20> 
setValue:forUndefinedKey:]: this class is not key value coding-compliant 
for the key actionText.'

There is my code here :

AlertCell.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>


@interface AlertCell : UITableViewCell {
    IBOutlet UILabel *actionText;
}

@property (retain, nonatomic) UILabel *actionText;

@end

And

AlertCell.m

@implementation AlertCell
@synthesize actionText;
   
- (void)dealloc {
    [actionText release];
    [super dealloc];
}
   
@end

The problem happens just there :

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    AlertCell *cell = 
      (AlertCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AlertCell" 
                                                         owner:nil 
                                                       options:nil];
        for (id oneObject in nib) {
            if ([oneObject isKindOfClass:[UITableViewCell class]]) {
                cell = (AlertCell *)oneObject;
                break;
            }
        }
    }    
    
    
    cell.actionText.text = [arrayAlert objectAtIndex:indexPath.row];
    return cell;
}

On this line :

NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AlertCell" 
                                             owner:nil 
                                           options:nil];

As asked, here is my header for the TableViewCOntroller :

#import <UIKit/UIKit.h>


@interface AlertesViewController : UITableViewController {
    NSMutableArray *arrayAlert;
}

And you can see my XIB file (as XML): http://pastebin.com/FDVzLYZu

@end

Can anyone help me ? Thanks a lot !

Community
  • 1
  • 1
Rob
  • 2,766
  • 5
  • 32
  • 39
  • Can you show your header file for your `UITableViewController` as well? I suspect that you are setting the wrong object as "File's Owner" for the `AlertCell` – falconcreek Sep 21 '10 at 14:26

8 Answers8

102

you probably based your code on a web tutorial such as the one at http://www.e-string.com/content/custom-uitableviewcells-interface-builder or http://www.icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/

Your problem (and I'm 99% sure this is where you tripped up, I just made the same mistake) is that in interface builder you linked your IBOutlets from File's Owner when you should link them from the cell view. This is why you are getting the errors.

theprof
  • 1,171
  • 1
  • 8
  • 4
  • 3
    THAT's the only enlightening answer among tons of misleading ones in this and other threads. THANKS! :D – Isaac Jan 20 '12 at 11:11
  • 1
    I followed that exact tutorial and had the exact problem. This fix worked for me. Thanks a lot :) – Tom Redman Jan 26 '12 at 21:37
  • WOW! I have been debugging this problem for some time. Your answer finally saved me ;). Thanks a lot. I have run into this problem before and went to very very extreme measures to work around it, eventhough I knew the methods to get it working were a bit ugly. THANKS!!! – 1amtoo1337 Dec 19 '12 at 01:04
  • 12
    Just to be perfectly clear about this: In IB, the "file owner" is going to refer to the ViewController that will show the custom cell. Usually you link interface objects to outlets there. NOT FOR CUSTOM CELLS! In the property inspector, select the custom cell, and set the "custom class" property to the class name that will have the outlets. Then attach interface objects to the .h of the class file, NOT File's Owner. – software evolved Jul 03 '13 at 04:44
  • Please accept this as the correct answer! The 2nd para with 2 lines is enough to solve this problem! By the way @theprof, thanks a lot! – Suran Nov 08 '13 at 12:03
  • Dear prof... this is me... from the future. I just wanted to let you know that you saved me a ton of time. Thank you. – michael Jul 08 '15 at 23:31
  • If I could give this more than one "plus one" I would. – Nostradamus Oct 04 '16 at 22:32
  • When I tried to connect interface elements to the .h file, it was connecting up to File's Owner for me. I had to go to the connections inspector for the table cell and connect the outlets to the interface elements that way. You can double-check by looking at the connections inspector for each interface element and making sure none of the referencing outlets refer to File's Owner. Xcode 9.3. – jpb May 01 '18 at 19:19
84

There are a couple of options to resolve this - i'll let you decide which is the most appropriate.

The reason it's failing is because the owner is being passed as nil. You're binding the actionText outlet to the file's owner in IB, but then when loading the nib, the owner is nil. I'd guess that when loading with a nil owner behind the scenes an NSObject is used, which is why you're getting the key/value error.

My previous advice to pass the cell as the owner would also fail as I didn't know how the Nib is constructed; the cell is nil as you've yet to create it (and dequeue is passing nil back, so even pass cell as the owner is still essentially passing nil).

Two options:

  • Instantiate a new cell in your -cellForRowAtIndexPath:(NSIndexPath *)indexPath implementation, and pass that new cell as the owner (but i'd guess that this isn't the best solution for you)
  • Or, and I'd suggest this is the better solution, change the binding of actionText in your nib file to the Alert Cell and not the file's owner (You have File's Owner and an Alert Cell - bind the UILabel to the actionText outlet of the Alert Cell, and not the File's owner, which is what's being done at present) - I suspect this is what you want. With that in mind file's owner can become an NSObject again.

------- Original answer kept below as the file's owner class is also a common cause for this error -------

It suggests that you've 'instantiated' an AlertCell in InterfaceBuilder, and you're binding something to actiontext, but the class isn't set to AlertCell, it's still NSObject?

Take a look at the class text box on the identify tab of the tool palette for that object in Interface Builder. The class should be AlertCell, but i'd guess it's still set to NSObject.

As an aside, and feel free to ignore this advice, but there are a couple of extra things i'd encourage you to do, purely from an Objective C expectations/conventions point of view:

  • Name your files after your class (upper case the first character of the filename).
  • Prefix your class names; two uppercase characters, typically your initials (i'd name it DWAlertCell, for example).
dannywartnaby
  • 5,512
  • 2
  • 21
  • 11
  • Unfortunately, I think that's not the problem. In IB, I can see the type of "File Owner" and "Alert Cell" set to "Alert Cell". Have you any another idea ? – Rob Sep 21 '10 at 13:48
  • Sorry for the typo error, my class files are capitalized ! I'll correct it soon. I'll try to prefix my class names, but I think that won't solve my current issue. – Rob Sep 21 '10 at 13:52
  • Truthfully i'm sure it's your IB config - i've hit this myself a couple of times. The other things i'd suggest (and without knowing the wider project, this may not be the cause), the owner of the loaded nib should really be the cell you've instantiated: [[NSBundle mainBundle] loadNibNamed:@"AlertCell" owner:cell options:nil]. Without an owner i'm not sure where the actionText outlet will be bound to. – dannywartnaby Sep 21 '10 at 13:56
  • Even if I set cell as the owner of the nib loaded, it failed :/ – Rob Sep 21 '10 at 14:05
  • Can you share your nib file? The problem is loading your nib, i'm convinced the problem is with the way it's configured in IB. – dannywartnaby Sep 21 '10 at 14:31
  • I added it as a pastebin :http://pastebin.com/FDVzLYZu – Rob Sep 21 '10 at 14:36
  • I've updated my original answer as it allows formatting - by copying your nib XML and creating appropriate classes I can get this to work by instantiating a cell as the owner, or by binding the action text outlet to the Alert Cell instead of the File's owner. – dannywartnaby Sep 21 '10 at 15:15
  • cheers, just had the exact same issue, been tearing my hair out for last couple of hours. SO ftw :) – gef Sep 21 '10 at 19:24
  • thanks for the answer it helped me. I was also stuck on same issue. Your answer helped me resolve my error. – pankaj Apr 26 '12 at 19:50
  • Dannywartnaby, thanks for posting your answer and sticking with it. I had an identical problem, in that I had tied my labels in a custom cell to the File's Owner rather than the Cell in the nib and I had owner:nil instead of owner:cell. I now have data appearing in my tableview. – Jazzmine Dec 14 '12 at 00:49
  • I believe the higher voted answer (below) is more correct than the accepted one – software evolved Jul 03 '13 at 04:45
  • Ancient Q&A, but still applicable in Swift. ;) I received this error when control-dragging my IB subview to my swift subclass of a `UITableViewCell` to create an IBOutlet. By default IB binds it to the File Owner, rather than the cell, causing it to try to look for my property on `NSObject` at runtime, resulting in the same error. The fix of course was to set it to the cell as you've pointed out. :) – qix Jan 02 '17 at 07:15
7

I had the same problem, and I could not find the mentioned variable/element in any file. I followed the following steps.

Environment: Xcode 6

  • Go to the Xib file, and right click (Ctrl+Mouse Click) on each UI element to see its associations.
  • In its referencing outlets section, you will be able to locate the erroneous association.
  • Remove that association
  • Clean the solution and build folder
  • Reset Simulator
  • Rebuild and run the project.
Deepend
  • 4,057
  • 17
  • 60
  • 101
Muhammad Umar
  • 151
  • 1
  • 5
5

check UIControl Referencing OutLet Property Name from nib/StoryBoard File

Baljeet Singh
  • 453
  • 5
  • 15
4

Make sure select cell to put custom class not File's Owner to add custom class on right side top and if You have done so, Before you make any changes remove outlet connection and then remove custom class from File's Owner and add Class to Cell view

3

Maybe it helps: Indentity Inspector when you have selected file's owner - UITableViewCell, when you have selected the cell inside Objects Your custom cell nane

Tatarasanu Victor
  • 656
  • 1
  • 7
  • 19
2

After 2 hours on this, I realized that i forgot to

@synthesize tableView = _tableView;

Hope it helps.

Mário

Mário Carvalho
  • 3,106
  • 4
  • 22
  • 26
0
#import <UIKit/UIKit.h>

@interface ReceptionDetailCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *productName;

@property (weak, nonatomic) IBOutlet UILabel *billType;

@property (weak, nonatomic) IBOutlet UILabel *bookTime;

@property (weak, nonatomic) IBOutlet UILabel *doctorName;

@property (weak, nonatomic) IBOutlet UILabel *downBillManName;




@end

Checkout out the Cell code and storyboard setting, whether they are all exist, maybe you have changed their names.

Gank
  • 4,507
  • 4
  • 49
  • 45