Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface Builder - How to create a custom UIView with many subviews

How can I create a custom UIView (with many subviews, UITextFields etc) in interface builder?

I don't want a viewController with NIB just a simple UIView, with lots of subviews, created in IB that I can then just alloc init and use, is this possible?

like image 503
Woodstock Avatar asked Dec 04 '22 09:12

Woodstock


1 Answers

Yes, you can create a UIView in a nib -- when you create a view based nib, that's what you're creating, a UIView. There is no view controller (though often, you make a view controller the File's Owner of the nib).

You would need to create a custom view class, and change the class of the view on the xib to that custom class, to hookup IBOutlets in that view. When you want to use the view in a controller, you can instantiate it like this:

UINib *nib = [UINib nibWithNibName:@"CustomView" bundle:nil];
CustomView *view = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];

The limitation of this method, is that your outlets belong to the view class and not the view controller, which may not (but could be) be the right thing to do in a MVC sense.

like image 187
rdelmar Avatar answered Dec 06 '22 21:12

rdelmar