views:

256

answers:

2

I'm loading a nib as:

ContentViewController *theController = [[ContentViewController alloc] initWithNibName:@"ContentView" bundle:nil];

which has a label on it. The view controller has an IBOutlet of UILabel with a @property of retain and synthesized variable. When I load the nib as above from another class and reference the label's text property as:

theController.myLabel.text = @"testing...";

myLabel has the address of 0x0. Before assignment, text is "invalid". After assignment, it's type is ContentViewController. There's some issue with memory management. Any ideas?

A: 

ViewControllers only load their views on demand. The implicit getMyLabel call in the assignment causes the view to be loaded and the outlet to be connected. As for the type error, I don't know why myLabel would end up with a type of ContentViewController. You should check the connections in interface builder and make sure they're hooked up properly.

n8gray
Outlets are connected properly. After above assignment, myLabel is still 0x0.
4thSpace
+1  A: 

The connections aren't made immediately in the init call. The main view isn't loaded until the view controller's view property is accessed, which causes all of your other IBOutlets to be set as well. If you are trying to access an IBOutlet before the view is loaded, it will be nil. Generally, assignment code should go into -viewDidLoad. If you need to do something before adding the view to the screen, just access the view before assigning anything to any of the IBOutlets:

theController.view;
theController.myLabel.text = @"testing...";
Ed Marty