pls
pls

Reputation: 1532

IBOutlets and Custom views

I have a UIViewController that requires 5 subviews which contain a UIImageView and a text UILabel.

I would like to have these as IBOutlet so have the following:

@property (nonatomic, weak) MyCustomView *customerView1
@property (nonatomic, weak) MyCustomView *customerView2
etc

I then have the following class which create these custom views in code:

#import "MyCustomView.h"

@implementation MyCustomView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        //[self setupView:frame andKeyColor:color];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame andKeyColor:(UIColor *)color
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setupView:frame andKeyColor:color];
    }
    return self;
}

- (void)setupView:(CGRect)frame andKeyColor:(UIColor *)color
{
    UIImageView *colorKeySquare = [self createColorKeySquare:frame andKeyColor:color];
    [self addSubview:colorKeySquare];

    UILabel *titleTextLabel = [self createTitleLabel:colorKeySquare.frame];
    self.titleText = titleTextLabel;
    [self addSubview:self.titleText];
}

- (UIImageView *)createColorKeySquare:(CGRect)frame andKeyColor:(UIColor *)color
{
    CGPoint point = CGPointMake(frame.origin.x, frame.origin.y);
    CGSize size = CGSizeMake(20, 20);
    UIImageView *colorKeySquare = [[UIImageView alloc] initWithFrame:CGRectMake(point.x, point.y, size.width, size.height)];

    [colorKeySquare setBackgroundColor:[UIColor redColor]];

    return colorKeySquare;
}

- (UILabel *)createTitleLabel:(CGRect)frame
{
    CGPoint point = CGPointMake(CGRectGetMaxX(frame), frame.origin.y);
    CGSize size = CGSizeMake(self.frame.size.width - frame.size.width, frame.size.height);
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(point.x, point.y, size.width, size.height)];

    return textLabel;
}

@end

I have hooked everything up correctly. However when I go to access the UILabel on one of my properties it always returns nil. The UILabel is not being allocated at any point.

If do the following: self.myCustomView1.titleLabel.text = @"Hello"; I get this when I print the object in the debugger:

Printing description of self->_nationalKey->_titleText:

What's the best way to go about setting up a custom view in code and having it associated with an IBOutlet?

Upvotes: 0

Views: 84

Answers (2)

dopcn
dopcn

Reputation: 4218

There are two ways to init a view:

  1. load from storyboard/xib

    In this case when you want access the view in code you need a IBOutlet

  2. init by code

    In this case you don't need a IBOutlet at all

    @property (nonatomic, strong) MyCustomView *customerView1
    

Upvotes: 0

Greg
Greg

Reputation: 25459

I don't know when you call initWithFrame:andKeyColor: but there are two main initialisers in UIView initWithFrame: which is called when you create view programmatically and initWithCoder: which is called when you create view in xib/storyboard.

In your example you should override initWithCoder:

- (id)initWithCoder:(NSCoder*)aDecoder 
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        [self setupControls];
    }

    return self;
}

Upvotes: 1

Related Questions