I usually create my custom views programatically and have them intended to be initialised programatically as well with custom init methods (e.g. initWithFrame:SomeParam:OtherParam). Is it possible to use such custom views in combination with a xib file? That is, having a parent xib file which has various of these custom views as subviews in which these subviews may need to use a different init method?
If you add custom views into a xib file, you can't use a custom initializer. They will all be initialized using initWithCoder
.
Typically you'd do any set up in a common method called from there, or in awakeFromNib
.
If you need to set any custom properties on your view which originate from outside it, do it in viewDidLoad
of your view controller.
Get the view's xib initialized the regular way and using the reference do the custom settings. this part can be put in the init method like this-
-(void)initfunction{
UIView *containerView = [[[NSBundle mainBundle] loadNibNamed:@"myView" owner:self options:nil] lastObject];
containerView.property1 = xyz;//Customization
containerView.property2= abc;//Customization
containerView.frame = CGRectMake(x,y,z,p);//Customization
[rootView addSubview:containerView];
}
the point is when we use a xib we do not initialize explicitly, the xib
utility functions return an initialized object(of UIView
), after one gets the UIView
object, he/she can use the initialized object as just a regular object to make further custom changes.
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