Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

ios7

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!

like image 955
Dmitry Sikorsky Avatar asked Nov 01 '13 17:11

Dmitry Sikorsky


People also ask

What is a key value in coding?

Key-value coding is a mechanism enabled by the NSKeyValueCoding informal protocol that objects adopt to provide indirect access to their properties. When an object is key-value coding compliant, its properties are addressable via string parameters through a concise, uniform messaging interface.

What is IBOutlet?

The type qualifier IBOutlet is a tag applied to an property declaration so that the Interface Builder application can recognize the property as an outlet and synchronize the display and connection of it with Xcode. An outlet is declared as a weak reference ( weak ) to prevent strong reference cycles.


1 Answers

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...

like image 107
Dmitry Sikorsky Avatar answered Dec 01 '22 00:12

Dmitry Sikorsky