Dmitry Sikorsky
Dmitry Sikorsky

Reputation: 1501

This class is not key value coding-compliant for the key... again

I have a details screen of some entity in my project (it is 'photo', actually), that can has comments. If it does, screen shows 3 the most recent and the button 'See all'. The problem was how to display that 3 comments. (On Android I simply use LinearLayout for that.) TableView is not suitable as I can see (due to has static height), so I decided to created my custom template/partial view CommentView and add it to scrollView for each comment.

I created XIB-file with view and some child controls (user photo, user name, date, text). Also, I created class CommentView, delivered from UIView. File's owner is set to CommentView. Class of the top-level view is also set to CommentView (I tried various combinations). I created outlet from top-level view (vContent) and from all of child views.

This is CommentView.h:

@interface CommentView : UIView

@property (strong, nonatomic) IBOutlet CommentView *vContent;
@property (weak, nonatomic) IBOutlet UIImageView *ivUserPhoto;
@property (weak, nonatomic) IBOutlet UILabel *lUserName;
@property (weak, nonatomic) IBOutlet UILabel *lCreated;
@property (weak, nonatomic) IBOutlet UILabel *lText;


@end

This is CommentView.m:

#import "CommentView.h"

@implementation CommentView

-(void)awakeFromNib {
    [[NSBundle mainBundle] loadNibNamed:@"CommentView" owner:self options:nil];
    [self addSubview: self.vContent];
}

@end

Then I tried to add comments into entity's view and got that error from question title. It's not the first time I got it but now I really don't know what the problem is...

Error occurs on this line (I call it from PhotoViewController):

CommentView *commentView = [[[NSBundle mainBundle] loadNibNamed:@"CommentView" owner:self options:nil]objectAtIndex:0];

enter image description here

The full text: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key ivUserPhoto.'

I really appreciate your help! Thank you!

Upvotes: 8

Views: 1747

Answers (2)

Dmitry Sikorsky
Dmitry Sikorsky

Reputation: 1501

I've found another one example here. So I rewrite my CommentView.m like this...

@implementation CommentView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self) {
        NSArray *nibContents = [[NSBundle mainBundle]loadNibNamed:@"CommentView" owner:self options:nil];
        [self addSubview:nibContents[0]];
    }

    return self;
}

@end

...and the way I'm adding subviews like this:

CommentView *commentView = [[CommentView alloc]initWithFrame:CGRectMake(0, 0, 320, 81)];

commentView.lUserName.text = comment.user.name;
[self.vComments addSubview:commentView];

Now It is working as I expected.

I still do not understand fully why there are a lot of examples like in my question and why it doesn't work for me... I hope my code will save time to someone...

Upvotes: 9

Ismael
Ismael

Reputation: 3937

That problem means your xib has some incorrect mapping. This generally happens when you change the name of a property.

To be sure you don't miss the problem, go to your xib file, disconnect ALL the outlets and map them again

Upvotes: 0

Related Questions