views:

693

answers:

2

When I programmatically allocated a UILabel in my custom initWithNibName method, and later in viewDidLoad, tried to assign a string to it, the label was not pointing to anything. I didn't release it; the label shows on the screen. If I create the label in IB, and assign text to it in viewDidLoad, it works.

Is it against a rule to set up manually allocated objects in viewDidLoad? Why is it not pointing to anything, even though viewDidLoad is called after my init?

From the doc of viewDidLoad:

This method is called after the view controller has loaded its associated views into memory. This method is called regardless of whether the views were stored in a nib file or created programmatically in the loadView method. This method is most commonly used to perform additional initialization steps on views that are loaded from nib files.

In my init:

_descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 218, 280, 10)];
 _descriptionLabel.numberOfLines = 0;
 _descriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
 _descriptionLabel.font = [UIFont systemFontOfSize:12.0];
 _descriptionLabel.adjustsFontSizeToFitWidth = NO;
 _descriptionLabel.text = @"Description not found.";
 _descriptionLabel.backgroundColor = [UIColor clearColor];

In viewDidLoad, the variable's value is 0x0. It's the same with my manually allocated UIButton, which is fully working once the view loads.

+1  A: 

If you want to create the UILabel programatically you can, but you still do it in viewDidLoad (as opposed to initWithNibName).

Don't be afraid to do UI setup in viewDidLoad. It is provided to add any static UI elements BEFORE the view appears on screen.

The view will not appear until just before viewDidAppear:(BOOL)animated is called.

If you have dynamic content configure it in viewWillAppear:(BOOL)animated. (This looks like your situation)

Then make sure you add it to the view:

[self.view addSubview:myLabel];

If you need future access to your new label, you will need to create an ivar to hold a pointer to it.

Corey Floyd
I moved my init code in viewDidLoad as suggested. I still don't understand why my initialized objects were pointing to 0x0 however..
hyn
Apple states that viewDidLoad should be used for additional setup. I have to re-read the docs, but this is probably one of the reasons why. The object probably isn't setup properly prior to viewDidLoad. I know I have also done setup in initWithCoder when initializing from a Nib, but I can't remember the situation specifically to comment
Corey Floyd
A: 

In the code posted, the _descriptionLabel UILabel is not retained and will be released before the view is drawn.

Try

_descriptionLabel = [[[UILabel alloc] initWithFrame:CGRectMake(20, 218, 280, 10)] retain];

Make sure you put [_descriptionLabel release] in dealloc. This assumes _descriptionLabel is an instance variable.

This basically applies to any object you create with alloc, copy, or new.

nessence
It does not need to be retained, it's allocated.
hyn