Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS custom views in xib files

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?

like image 785
KTas Avatar asked Jun 13 '12 00:06

KTas


2 Answers

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.

like image 78
jrturton Avatar answered Nov 15 '22 18:11

jrturton


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.

like image 31
Ishank Avatar answered Nov 15 '22 19:11

Ishank