不能加载带有Nib(.xib)的自定义UIView的子视图? [英] Subviews for custom UIView with Nib (.xib) don't load?
问题描述
我正在尝试创建一个带有关联的Nib(.xib)文件的自定义UIView类,以帮助进行布局和创建.
I am trying to create a custom UIView class with an associated Nib (.xib) file to help with layout and creation.
现在,即使子视图存在于我的nib文件中,并且已通过IBOutlets正确连接到我的自定义视图类,也不会在我的自定义视图中显示子视图.
Right now, no subviews in my custom view appear, even though they exist in my nib file and are hooked up properly to my custom view class via IBOutlets.
我尝试通过以下其他答案: 从视图加载带有XIB的自定义UIView使用IB的控制器视图
I tried going through the following other answer: Load custom UIView with XIB from a View Controller's view using IB
和这个: 和这个: http://soulwithmobiletechnology. blogspot.com/2011/06/create-uiview-with-nib-file-in-xcode-4.html
,但是方法[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil]
似乎会调用-[initWithCoder:]
,从而导致无限循环.
but the method [[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil]
seems to call -[initWithCoder:]
, leading to an infinite loop.
请让我知道要包括哪些其他详细信息来帮助解决此错误!
Please let me know what other details to include to help solve this bug!
更新1:我是否需要通过-[layoutSubviews]
布置子视图?我的笔尖文件实际上没有布置吗?如果是这样,那么nib文件的目的是什么?
UPDATE 1: Do I need to lay out my subviews via -[layoutSubviews]
? Does my nib file actually not lay it out? If so, then what is the point of the nib file?
推荐答案
打开任何视图控制器,并将此代码粘贴到您的viewDidAppear
方法中:
Open any of your view controllers and paste this code into your viewDidAppear
method:
NSArray * allTheViewsInMyNIB = [[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil]; // loading nib (might contain more than one view)
MyCustomView* problemView = allTheViewsInMyNIB[0]; // getting desired view
problemView.frame = CGRectMake(0,0,100,100); //setting the frame
[self.view addSubview:problemView]; // showing it to user
这就是呈现视图所需要做的一切.如果要与此视图进行交互,则必须在类的接口中声明MyCustomView* problemView
,而不是在方法中声明.
That's all you have to do, to present a view. If you want to interact with this view — you have to declare MyCustomView* problemView
in your class' interface, not in the method.
这篇关于不能加载带有Nib(.xib)的自定义UIView的子视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!