Problem in adding subview in UIWindow

Total Post:51

 830  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?

  1. Post:214

    Re: Problem in adding subview in UIWindow

    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