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

戏子无情 提交于 2019-12-18 11:41:01

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/18109741/interface-builder-how-to-create-a-custom-uiview-with-many-subviews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!