I donโt use Interface Builder in my projects, and I noticed one thing that I donโt know how to explain. Yet. So to the point. When we use IB and define user interface elements such as UILabel or UIButton in our controller, we use this ugly IBOutlet prefix and a "weak" modifier. It works like music. But when we decide not to use IB and define the entire user interface from the code, it just doesn't work.
Suppose I want to add a UILabel to a controller (using IB). I will have something like this file I * .h:
@property (nonatomic, weak) IBOutlet UILabel * label;
And I do not need to do anything in the * .m file. But if I delete the * .xib file and try to configure my UILabel in, for example, one of the init methods, for example:
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,20)]; self.label.text = @"some text"; [self.view addSubview:self.label];
This does not work until I redo my * .h file:
@property (nonatomic, strong) UILabel * label;
Now, I know the difference between weak and strong, but I have no idea why we can use weak ui elements when using IB? Something must contain strong indications of these elements, right? But what?? In the second case, this is the controller, but I do not understand how it behaves in the first case.
source share