views:

494

answers:

1

The property label is of type UILabel and is an outlet in UIViewController.

Why does the following work :-

[window addSubview:viewController.view];
[viewController.label setText:@"New Label"] ;

and the reverse sequence of statements doesn't change the default text in the label:

[viewController.label setText:@"New Label"] ;
[window addSubview:viewController.view];

Isn't view loaded from the UI elements attached to the UIViewController ?

+2  A: 

A view controller creates or loads its view when it's first requested, either from a NIB or via the loadView method. And in most cases, the associated subviews are also only created when the main view is loaded. That means that viewController.label is nil until you've first tried to access viewController.view.

So if you try to set the label text first, you're actually just sending that message to nil, which is why it doesn't appear to have taken effect.

Daniel Dickison