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?

+1


a source to share


3 answers


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...";

      

+3


a source


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.



0


a source


Does any code write,

-(void)loadView {  }

      

method?

If so, the IBOutlet object does not receive a selection.
Try commenting out this function.
And if you need this function, you get a fired call to the loadNibnamed: method to call that viewController.

0


a source







All Articles