使用 Xib 将 UIView 添加到 UIViewController
这就是我想要做的:
- 我有
AViewController
,它是 Xib,然后我添加一个UIView
一半的屏幕,我将此视图连接到 B-UIView 和 B- UIView 有自己的 xib,我在那里进行设计。
换句话说,我有一个视图控制器,它的视图成为“主视图”,因为我向其中添加了带有自己的 xib 的其他视图。
层次结构:
1 ViewController
UIView (xib) 2.B-UIView(有自己的xib) 3.C-UIView(有自己的xib)
那么这可能吗?
非常感谢!
Here is what i am trying to do:
- I have
AViewController
and it's Xib, then I add aUIView
half the screen which I connect this view to B-UIView and B-UIView has its own xib where i do the design.
In other words I have a view controller with it's view which becomes the "mainview" as I add other views to it which come with its own xibs.
Hierarchy:
1 ViewController
UIView (xib)
2.B-UIView (has its own xib)
3.C-UIView (has its own xib)
So is this possible if so how?
Many thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,你绝对可以做到,而且一点也不难!
事实上,这是在 Interface Builder 中设计某些东西的一种非常常见的方法。然后,您可以使用
loadNibNamed
以编程方式加载自定义 Xib 文件。例如:Apple 的文档在“iOS 的表视图编程指南”中讨论了这一点。
尽管他们的示例是关于创建自定义表格单元格,但相同的规则适用于任何视图。仔细查看标题为 从 Nib 文件加载自定义表格视图单元。
在该示例中他们没有做的一件事是加载顶级视图。
loadNibNamed
返回一个数组,因此要获得您想要的顶级视图,您还应该这样做:因此,您的代码将如下所示:
Yes, you can definitely do this and it's not difficult at all!
In fact, it's a pretty common way to design something in Interface Builder. You can then load your custom Xib file programmatically using
loadNibNamed
. For example:Apple's docs talk about this in the "Table View Programming Guide for iOS".
Although their example is about creating a custom table cell, the same rules apply for any view. Have a close look at the section entitled Loading Custom Table-View Cells From Nib Files.
One thing they don't do in that example is to load the top level view.
loadNibNamed
returns an array, so to get that top level view that you're after, you should also do:So, your code will look like: