如何在UIView中加载xib文件 [英] How to load a xib file in a UIView
问题描述
我一直在各处搜索,到目前为止没有任何东西对我有用。
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中加载我的firstView.xib在containerView中,但由于它只占用了屏幕的一半,你仍然可以看到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:@MyXibNameowner: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.
所以,你可以这样做:
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屋!