views:

101

answers:

3

MyController *myViewController = [[MyController alloc] initWithNibName:@"myView" bundle:nil];

The nib file myView.nib has 2 uiimageviews and 2 uilabels. When I first init myViewController, all the 4 subviews are set as 0x0. The second time I dont get such a behavior.

A: 

You probably forgot to hook up the view in your Nib file to the view property of MyController, and/or hooked up the subviews to the various IBOutlets of MyController.

Shaggy Frog
how do you hook up the view in the nib file to the view property of the View Controller?
Minar
+3  A: 

The view object itself does not get created until it is referenced via self.view and loadView is called. It could be that the first time you try to inspect the view or do something with it this hasn't happened yet, and the second time could be after the system creates the view if you are adding it to another view or a navigation controller or something.

Kevlar
A: 

Kevlar is absolutely right. You can force loading view and setting up all references with the following statement:

if (myViewController.view);

It does nothing except you'll get all subviews bound to outlets.

Valerii Hiora
How does this statement work? Can you please explain?
Minar
It is empty statement.Accessing myViewController.view automatically loads view and that's a trick.
Valerii Hiora