PROBLEM IN ADDING SUBVIEW IN UIWINDOW

Hugh Jackman

Total Post:52

Points:366
Posted by  Hugh Jackman
 605  View(s)
Ratings:
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. Tarun Kumar

    Post:214

    Points:1642
    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!