Darren
Darren

Reputation: 10129

Setting text for IBOutlet labels in a custom UIView

I have a custom UIView (MyView) object that gets loaded from a xib file. In the MyView.m I have a method that gets called in initWithCoder that does some initialization. This works for the most part, but I was trying to set the text in some IBOutlet label properties here, and it wasn't working, I realized because the properties were not initialized at this point.

I tried setting the text of the outlets in the drawRect method, and it does work. But I think I read that implementing drawRect if you don't need to can cause some performance hits. My question is, where would the best place to set text of my IBOutlet label properties? (What I'm actually doing is setting it to localized versions of what's in the xib. I know about localized xib files, but I want to set the text in code.)


Here is some code. Here is how I initialize the view in my view controller:

NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"MyView"
                                                     owner:self
                                                   options:nil];
_myView = [nibContents objectAtIndex:0];

Here is the initWithCoder method in MyView.m

- (id)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self localizeLabels];
    }
    return self;
}

And here is the localizeLabels method:

- (void)localizeLabels
{
    self.powerPlant.text = NSLocalizedString(@"POWERPLANTBREAKDOWN_HEADERCOLUMN1", nil);
    self.output.text = NSLocalizedString(@"POWERPLANTBREAKDOWN_HEADERCOLUMN2", nil);
    self.capacity.text = NSLocalizedString(@"POWERPLANTBREAKDOWN_HEADERCOLUMN3", nil);
}

I have checked that these methods do get called using breakpoints, and that the properties are nil in the localizeLabels method.

Upvotes: 1

Views: 1790

Answers (2)

Dinesh Raja
Dinesh Raja

Reputation: 8501

UPDATED: Your MyView.m class should be like this in the start.

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"MyView"
                                                         owner:self
                                                       options:nil];
        UIView *_myView = [nibContents objectAtIndex:0];
        [self addSubview: _myView];
        [self localizeLabels];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Initialization code
        [self localizeLabels];
    }
    return self;
}

and in your viewcontroller. you need to initialize your MyView like this.

MyView _view = [[MyView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:_view];

Upvotes: 2

foundry
foundry

Reputation: 31745

Your IBOutlet properties are initialized if they are properly wired up to the xib in Interface Builder. The initialisation takes place as the xib is loaded into the app. These properties are only weak references to xib-instantiated objects after all.

So my best guess is that you have not wired up the IBOutlets to their respective xib objects - check in Interface Builder.

Upvotes: 0

Related Questions