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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:35:06  来源:igfitidea点击:

Custom UIView added in the interface builder does not load the xib

iosiphoneobjective-cuiview

提问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:

认为:

  1. Created a UIViewsubclass called CustomXIBView
    • (New File > Cocoa Touch > Objective-C Class -- Subclass of UIView)
  2. Created a Simple View User Interface and named it CustomXIBView
    • (New File > User Interface > View)
  1. 创建了一个UIView名为的子类CustomXIBView
    • (新文件 > Cocoa Touch > Objective-C 类 -- 的子类UIView)
  2. 创建一个简单的视图用户界面并命名 CustomXIBView
    • 新文件 > 用户界面 > 查看

Steps:

脚步:

  1. Go to CustomXIBView's nib
  2. Select View(left toolbar)
  3. Select Show Identity Inspector(3rd option in the panel on the right)
  4. Specify CustomXIBViewas the Custom Classof the View
    • don't do anything with CustomXIBView's File's Ownerin the nib
  5. Drag Drop Objects and connect it with CustomXIBView.h
  1. CustomXIBView的笔尖
  2. 选择View左侧工具栏
  3. 选择Show Identity Inspector右侧面板中的第三个选项
  4. 指定CustomXIBView作为自定义类View
    • 不要对笔尖中的CustomXIBView's做任何事情File's Owner
  5. 拖放对象并将其与 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];


ref: http://eppz.eu/blog/uiview-from-xib/

参考:http: //eppz.eu/blog/uiview-from-xib/