IB objects versus hand-selected objects in init / viewDidLoad
When I programmatically highlighted the UILabel in my normal initWithNibName method, and later in viewDidLoad, tried to assign a string to it, the shortcut didn't point to anything. I have not released it; the shortcut is displayed on the screen. If I create a shortcut in IB and assign text to it in viewDidLoad it works.
Is this against the rule for manually setting selected objects in viewDidLoad? Why doesn't this point to anything even though the viewDidLoad is called after my init?
From the dodLoad doc:
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 saved in a nib file or created programmatically in the loadView method. This method is most commonly used to perform the extra steps of initializing views 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 value of the variable is 0x0. It's the same with my hand-selected UIButton, which works completely after loading the view.
a source to share
If you want to create a UILabel programmatically, you can, but you still do it in viewDidLoad (as opposed to initWithNibName).
Feel free to do your UI customization in viewDidLoad. It is provided to add any static interface elements before the view appears on the screen.
The view will not be rendered until the animated view viewDidAppear: (BOOL) is called.
If you have dynamic content, set it up in viewWillAppear: (BOOL) animated. (This is similar to your situation)
Then make sure you add it to the view:
[self.view addSubview:myLabel];
If you need future access to your new shortcut, you will need to create an ivar to hold a pointer to it.
a source to share
In the code posted, the _descriptionLabel UILabel is not saved 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 that _descriptionLabel is an instance variable.
This basically applies to any object you create with alloc, copy, or new.
a source to share