I have a custom view to which I need to add a couple of subviews programmatically. Which is the best place to create and add these subviews? Apple docs say:
If your view class manages one or more integral subviews, do the following: Create those subviews during your view’s initialization sequence.
This is a bit unclear to me. Should I handle that in initWithFrame
, initWithCoder
or somewhere else?
Note that I'm not talking about controllers here, this is a View that needs to initialize itself.
initWithFrame
is the method you call to create a UIView programmatically, while initWithCoder
is called when a UIView is instanciated from a XIB.
So it all depends on how you're going to create your containing view. A way to cover all cases :
- (id) initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])){
[self setUpSubViews];
}
return self;
}
- (id) initWithCoder:(NSCoder*)aDecoder
{
if ((self = [super initWithCoder:aDecoder])){
[self setUpSubViews];//same setupv for both Methods
}
return self;
}
- (void) setUpSubViews
{
//here create your subviews hierarchy
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With