Interface Builder - 如何创建具有许多子视图的自定义UIView

Interface Builder  - 如何创建具有许多子视图的自定义UIView

问题描述:

如何在界面生成器中创建自定义UIView(包含许多子视图,UITextField等)?

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

我不想让一个viewController与NIB只是一个简单的UIView ,有很多subview,在IB中创建,然后我可以只分配init和使用,这是可能吗?

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?

你可以在一个nib中创建一个UIView - 当你创建一个基于视图的nib,这就是你正在创建的UIView。没有视图控制器(虽然经常,你创建一个视图控制器的文件的所有者的nib)。

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).

你需要创建一个自定义视图类,类的视图在xib到该自定义类,以联接在该视图中的IBOutlets。当你想在控制器中使用视图时,你可以像这样实例化:

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];

此方法的局限性是,您的插座属于视图类而不是视图控制器,这可能不是(但可能是)在MVC意义上做正确的事情。

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.