ios 界面生成器中添加的自定义UIView不加载xib
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20309767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom UIView added in the interface builder does not load the xib
提问by Luda
I have created custom UIViewwith a xib.
我UIView用 xib创建了自定义。
In addition I have a UIViewControllerin my storyboard, to which I have added a UIViewand set its class to be my custom UIView.
此外UIViewController,我的故事板中有一个,我添加了一个UIView并将其类设置为我的自定义UIView.
But when I am running the app, the view doesn't have its subviews. When I debug, all the subviews are null.
但是当我运行该应用程序时,该视图没有其子视图。当我调试时,所有子视图都为空。
In .m of the custom UIView, these init methods are present:
在 custom 的 .m 中UIView,存在这些 init 方法:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
}
return self;
}
What am I missing?
我错过了什么?
回答by staticVoidMan
As you already know, with a UIViewControllerwe have the -initWithNibName:bundle:method to connect it with an xib.
But...
When it comes to a UIView, you need to use -loadNibNamed:owner:options:and load it with the xib. (simply specifying a custom class to the view in the xib won't work)
正如您已经知道的那样,UIViewController我们可以通过 a-initWithNibName:bundle:将其与 xib 连接起来。
但是...
当涉及到 a 时UIView,您需要使用-loadNibNamed:owner:options:xib 并加载它。(简单地为 xib 中的视图指定一个自定义类是行不通的)
Suppose:
认为:
- Created a
UIViewsubclass calledCustomXIBView- (New File > Cocoa Touch > Objective-C Class -- Subclass of
UIView)
- (New File > Cocoa Touch > Objective-C Class -- Subclass of
- Created a Simple View User Interface and named it
CustomXIBView- (New File > User Interface > View)
- 创建了一个
UIView名为的子类CustomXIBView- (新文件 > Cocoa Touch > Objective-C 类 -- 的子类
UIView)
- (新文件 > Cocoa Touch > Objective-C 类 -- 的子类
- 创建一个简单的视图用户界面并命名
CustomXIBView- (新文件 > 用户界面 > 查看)
Steps:
脚步:
- Go to
CustomXIBView's nib - Select
View(left toolbar) - Select
Show Identity Inspector(3rd option in the panel on the right) - Specify
CustomXIBViewas the Custom Classof theView- don't do anything with
CustomXIBView'sFile's Ownerin the nib
- don't do anything with
- Drag Drop Objects and connect it with
CustomXIBView.h
- 去
CustomXIBView的笔尖 - 选择
View(左侧工具栏) - 选择
Show Identity Inspector(右侧面板中的第三个选项) - 指定
CustomXIBView作为自定义类的View- 不要对笔尖中的
CustomXIBView's做任何事情File's Owner
- 不要对笔尖中的
- 拖放对象并将其与
CustomXIBView.h
Code:
代码:
//To load `CustomXIBView` from any `UIViewController` or other class:
//instead of the following commented code, do the uncommented code
//CustomXIBView *myCustomXIBViewObj = [CustomXIBView alloc] init];
//[myCustomXIBViewObj setFrame:CGRectMake(0,0,320,480)];
//Do this:
CustomXIBView *myCustomXIBViewObj =
[[[NSBundle mainBundle] loadNibNamed:@"someView"
owner:self
options:nil]
objectAtIndex:0];
[myCustomXIBViewObj setFrame:CGRect(0,
0,
myCustomXIBViewObj.frame.size.width,
myCustomXIBViewObj.frame.size.height)];
[self.view addSubview:myCustomXIBViewObj];

