Home > DeveloperSection > Forums > Problem in adding subview in UIWindow
Hugh Jackman
Hugh Jackman

Total Post:52

Posted on    November-20-2015 1:07 AM

 iPhone iPhone  Ios  Objective C 

 1 Reply(s)
 567  View(s)
Rate this:

I have created an @property of UILabel and an outlet of UIViewController,
this is the code which works perfectly:

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

but when we write this code in reverse order, it generates error:

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

why it generate error?

Tarun Kumar

Total Post:214

Posted on    November-20-2015 10:24 PM

A view controller creates or loads its view when it's first requested from a NIB or via the loadView method.
In most cases, the associated subviews 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 we will try to set the label text first, we are actually just sending that message to nil, which is why it doesn't appear to have taken effect

Modified On Nov-20-2015 10:24:56 PM

Don't want to miss updates? Please click the below button!

Follow MindStick