eugene
eugene

Reputation: 41685

viewController nib's view is nil?

I used to create viewController/views programmatically only.

Using xib is harder than I expected.

I found that self.view of an xib or any other subview is initially nil.

I created the viewController by [[MyViewController alloc] init]  
and tried to [myViewController.imageView setImage: image];  //imageView is nil  

NSLog(@"%p", myViewController.view); // access view here
[myViewController.imageView setImage: image]; //works now 

I feel I'm missing something very basic. What would it be?

Upvotes: 0

Views: 1169

Answers (3)

Bajaj
Bajaj

Reputation: 869

Try

[[MyViewController alloc] initWithNibname:@"mynibname" bundle:nil] 

in this format to allocate the proper view ....try it

Upvotes: 0

Jim
Jim

Reputation: 73936

As described in the documentation for UIViewController, the views are lazily loaded when you first access the view property. The earliest you can reference them is in viewDidLoad.

Upvotes: 1

phoganuci
phoganuci

Reputation: 5064

In my experience this happens when I try to access the view before viewDidLoad fires, for instance trying to access this in the init method of the view controller. If this is the case, wait until after viewDidLoad fires to access the view.

Upvotes: 0

Related Questions