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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18109741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:40:35  来源:igfitidea点击:

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

iosobjective-cxcodecocoa-touchinterface-builder

提问by Woodstock

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

如何在界面构建器中创建自定义 UIView(带有许多子视图、UITextFields 等)?

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 的 viewController 只是一个简单的 UIView,有很多子视图,在 IB 中创建,然后我可以分配初始化并使用,这可能吗?

回答by rdelmar

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

是的,您可以在笔尖中创建 UIView——当您创建基于视图的笔尖时,这就是您正在创建的 UIView。没有视图控制器(尽管通常,您将视图控制器设为笔尖的文件所有者)。

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:

您需要创建一个自定义视图类,并将 xib 上的视图类更改为该自定义类,以在该视图中连接 IBOutlets。当你想在控制器中使用视图时,你可以像这样实例化它:

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.

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