Why UILabel 0x0?
I am loading the thread as:
ContentViewController *theController = [[ContentViewController alloc] initWithNibName:@"ContentView" bundle:nil];
which has a label. The view controller has an IBOutlet UILabel with @property stored 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 address 0x0. Before the assignment, the text is "invalid". Once assigned, it is of type ContentViewController. There's a memory management issue in there. Any ideas?
a source to share
Connections are not made immediately in the init call. The main view is not loaded until the view manager property is accessed view
, which will also set all other IBOutlets. If you try to access the IBOutlet before loading the view, it will be zero. Typically, the destination code should go to -viewDidLoad
. If you need to do something before adding a view to the screen, just enter the view before assigning anything to any of the IBOutlets:
theController.view;
theController.myLabel.text = @"testing...";
a source to share
ViewControllers only load their views on demand. An implicit call getMyLabel
in an assignment causes the view to load and plug into an outlet. As for the type error, I don't know why it myLabel
will have a type ContentViewController
. You have to check the connections in the interface builder and make sure they are connected correctly.
a source to share