Reputation: 8980
Consider a situation where you have an object in a storyboard file linked up to an IBOutlet
, such as the following.
@property (nonatomic, strong) IBOutlet UIImageView *imageView;
Left to its own devices, the ViewController
will happily load and display the correct image inside the correct frame, etc, as determined in Interface Builder.
However, if the following code is added to the View Controller's viewDidLoad
method, an (apparently) identical outcome is achieved.
- (void)viewDidLoad {
_imageView = [[UIImageView alloc] init];
//...
}
Does this mean the programmatic alloc-initing of IBOutlets
is completely optional? Or is the addition of this line achieving a slightly different outcome?
I'm asking because I have always added the alloc-init in the implementation, then in my latest project I forgot about it, and to my surprise, it still worked exactly as intended.
Upvotes: 1
Views: 610
Reputation: 119272
A storyboard or xib file is a serialisation of the objects contained within. When the storyboard is loaded, all of the objects are instantiated (using initWithCoder:
, since we are deserialising them). This does the same job as allo/init, with the addition of setting all of the properties of the object that are defined in the xib.
The xib contains enough information about your image view to fully create it. If you then alloc/init a new image view and assign it to the variable that was used for the outlet, you are just replacing the object that was created from the storyboard, which is pretty pointless, since your outlet now points to nothing on the screen.
In summary - alloc/init ing every outlet isn't optional, it's wrong.
Upvotes: 6
Reputation: 4705
Alloc/init of an object in storyboard is basically a mistake.
When the view is loaded, all the subviews will be initialized and loaded automatically. If you initialize it again, there will be another instance created and the previous instance of the object goes unused.
In fact, in order to make the recently initialized object instance to work properly, you have to add the instance (in your case instance of UIImageView) as subview.
Upvotes: 0
Reputation: 229
When you add an object to a NIB the life cycle of the object its completely controlled by the UIViewController (or subclass) and the ARC reference it's weak by default as the ViewController will handle the alloc, init and release of the object properly when loading the view, so what your are doing there its changing the ARC reference to strong, so the object doesn't gets immediately released after your reference it, it will work as you tried but in theory you will end with the object created in Interface Builder and the one you just create in there, if you didn't set any frame or added to the any view and you don't make any reference posterior to the object ARC will release it, and you will end with the image view you create in Inteface Builder, but without any pointer to it as you reference _imageView to the new [[UIImageView alloc] init] you just created.
Upvotes: 2
Reputation: 2616
If you are using Xcode 4.4 it will automatically synthesize the properties for you. It may be this you are experiencing? Ref
Upvotes: 1