---
title: "Problem in adding subview in UIWindow"  
description: "Problem in adding subview in UIWindow"  
author: "Anonymous User"  
published: 2015-11-20  
updated: 2015-11-20  
canonical: https://www.mindstick.com/forum/33617/problem-in-adding-subview-in-uiwindow  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 1 minute  

---

# Problem in adding subview in UIWindow

I have created an @[property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) of UILabel and an outlet of UIViewController,\
this is the [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) which works perfectly:

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

but when we write this code in [reverse order](https://www.mindstick.com/forum/156836/what-is-a-c-sharp-program-to-print-individual-from-a-string-in-reverse-order), it generates [error](https://yourviews.mindstick.com/view/88527/fixing-quickbooks-error-4120-reinstalling-vs-repairing):

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

why it generate error?

## Replies

### Reply by Tarun Kumar

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


---

Original Source: https://www.mindstick.com/forum/33617/problem-in-adding-subview-in-uiwindow

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
