Shayno
Shayno

Reputation: 808

How to set values of subclassed UIView properties when loading subclass from a xib

I have a subclassed UIView called BannerHeaderView, and designed the way I want it layed out in an xib. I have set the xib class to BannerHeaderView. I have a UILabel and a UIImageView which I have connected in IB.

The problem is that when I go to set the values of these subViews after initialising my BannerHeaderView in a ViewController, the label and image view are null. I have tried setting them in init also.

Code for BannerHeaderView.h:

@interface BannerHeaderView : UIView
@property (strong, nonatomic) IBOutlet UIImageView *bannerImage;
@property (strong, nonatomic) IBOutlet UILabel *headerLabel;

-(void)setTitle:(NSString *)title;
-(void)setImageFile:(PFFile *)imageFile;

@end

Code for BannerHeaderView.m:

@implementation BannerHeaderView
@synthesize headerLabel, bannerImage;

-(id)init{
    self = [super init];
    if (self) {
        [self customInit];
    }
    return self;
}

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self customInit];
    }
    return self;
}

-(void)customInit{
    UIView *contentView = [[[NSBundle mainBundle] loadNibNamed:@"BannerHeaderView"
                                   owner:self
                                 options:nil] objectAtIndex:0];
    [self addSubview:contentView];
    contentView.frame = self.bounds;
}

-(void)setTitle:(NSString *)title{
    NSLog(@"HEADER = %@", headerLabel);
    headerLabel.text = title;
}

And I am creating the BannerHeaderImage in my ViewController like so:

bannerHeader = [[BannerHeaderView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, bannerHeight + segmentedHeaderHeight)];
[bannerHeader setTitle:@"I AM THE NEW TITLE"];

The log tells me that the headerLabel is null.

Questions:

How do I set the values of these subviews? Should I just design the layout of this programatically?

Thanks for your time.

P.S. I have spent hours researching this and I can't find a solution. I did find similar questions on SO but none with suitable answers to enable me to solve my problem...

Upvotes: 0

Views: 31

Answers (1)

Ashwin Sathawane
Ashwin Sathawane

Reputation: 381

Please set the class BannerHeaderView to File's Owner like this

File owner

Upvotes: 2

Related Questions