如何在 UIView 中加载 xib 文件 [英] How to load a xib file in a UIView

查看:22
本文介绍了如何在 UIView 中加载 xib 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处寻找,但到目前为止没有任何效果.

I have been searching everywhere and nothing so far has worked for me.

基本上我想要一个名为 rootView.xib 的 .xib 文件,在里面我想要一个 UIView(我们称之为 containerView),它只占屏幕的一半(所以会有常规视图和一个新的看法).然后我想要一个名为 firstView.xib 的不同 .xib 文件并将其加载到 containerView 中.所以我可以在 firstView.xib 上有一堆东西,在 rootView.xib 里有一堆不同的东西,并在 rootView.xib 的 containerView 中加载我的 firstView.xib 但因为它只占屏幕的一半,你仍然会看到rootView.xib 上的东西

Basically I want to have a .xib file called rootView.xib and inside it I want to have a UIView (lets call it containerView) that takes up only half of the screen (so there would be the regular view and a new view). Then I want a different .xib file called firstView.xib and load it inside of containerView. So I can have a bunch of stuff on firstView.xib and a bunch of different stuff in rootView.xib and load my firstView.xib inside of containerView in rootView.xib but since it only takes up half of the screen you would still see the stuff on rootView.xib

推荐答案

要以编程方式从 xib 文件中获取对象,您可以使用: [[NSBundle mainBundle] loadNibNamed:@"MyXibName" owner:self options:nil] 返回 xib 中顶级对象的数组.

To get an object from a xib file programatically you can use: [[NSBundle mainBundle] loadNibNamed:@"MyXibName" owner:self options:nil] which returns an array of the top level objects in the xib.

所以,你可以做这样的事情:

So, you could do something like this:

UIView *rootView = [[[NSBundle mainBundle] loadNibNamed:@"MyRootView" owner:self options:nil] objectAtIndex:0];
UIView *containerView = [[[NSBundle mainBundle] loadNibNamed:@"MyContainerView" owner:self options:nil] lastObject];
[rootView addSubview:containerView];
[self.view addSubview:rootView];

这篇关于如何在 UIView 中加载 xib 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆