1
votes

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.

2
Could you show some example code that does not work? I don't know what you mean by "the label was not pointing to anything"stefanB
Edited original post for clarification.hyn
Have you created the UILabel in IB e.g. is it defined as IBOutlet UILable and it is connected to a UILabel on window in IB, or are you trying to create it pragmatically e.g. UILable * descLabel = [[... ]]?stefanB
If I create it in IB and connect the outlets, I can access the label in viewDidLoad. But not if I programmatically allocate it in init. I need to create this one label manually because of an IB limitation. I can of course populate the label in init, and that's probably the right way to do it. But I was just wondering why I can't access the label in viewDidLoad.hyn
In appDoc initWithNibName:bundle: If you specify a nib name and need to set values after the nib file is loaded, then you should override the viewDidLoad method to do so. Why you try to do it in init? Yo must do it in viewDidLoadoxigen

2 Answers

1
votes

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.

0
votes

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.