简体   繁体   English

如何将视图XIB作为子视图加载到视图控制器XIB中

[英]How to load a view XIB as a subview into a view controller XIB

I have a custom view I made with an IB file since it's quite complex: (RotatorView.xib/.h/.m). 我有一个我用IB文件制作的自定义视图,因为它非常复杂:(RotatorView.xib / .h / .m)。 I want to add the RotatorView as a subview to a view controller. 我想将RotatorView作为子视图添加到视图控制器中。 I can do this programmatically using: 我可以使用以下方式编程:

NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"RotatorView" 
                                               owner:self options:nil];

But I'd rather not do that. 但我宁愿不这样做。 It would be better for my design if I could add the RotatorView to the canvas of a View Controller in Interface Builder. 如果我可以将RotatorView添加到Interface Builder中的View Controller的画布上,那对我的设计会更好。 That way I could use IB to manage the properties of each instance of RotatorView instead of setting properties programmatically in each class. 这样我可以使用IB来管理RotatorView的每个实例的属性,而不是在每个类中以编程方式设置属性。 Is there a way that I can add my custom view with XIB to a parent view controller? 有没有办法可以将我的自定义视图与XIB一起添加到父视图控制器? Do I have to build an IB plugin to do this? 我是否必须构建一个IB插件才能执行此操作?

You can add a UIView control to the canvas in IB and change its class name to match your custom view class name. 您可以将UIView控件添加到IB中的画布并更改其类名以匹配您的自定义视图类名。 Although this will save you from calling loadNibNamed method programmatically, but you'll not be able to set the custom properties directly from IB. 虽然这将节省您以编程方式调用loadNibNamed方法,但您将无法直接从IB设置自定义属性。

The only solution for doing this is to build an IB plugin , but unfortunately it seems you can't make Cocoa Touch IB plugins because you can't make Cocoa Touch frameworks. 这样做的唯一解决方案是构建一个IB插件 ,但不幸的是,你似乎无法制作Cocoa Touch IB插件,因为你无法制作Cocoa Touch框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM