在界面构建器中添加的自定义UIView不会加载xib

时间:2022-09-06 20:10:23

I have created custom UIView with a xib.

我用xib创建了自定义UIView。

In addition I have a UIViewController in my storyboard, to which I have added a UIView and 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.

但是当我运行应用程序时,视图没有其子视图。当我调试时,所有子视图都为null。

In .m of the custom UIView, these init methods are present:

在自定义UIView的.m中,存在以下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?

我错过了什么?

1 个解决方案

#1


31  

As you already know, with a UIViewController we 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,我们使用-initWithNibName:bundle:方法将其与xib连接。但是......当谈到UIView时,你需要使用-loadNibNamed:owner:options:并用xib加载它。 (只是在xib中为视图指定自定义类将不起作用)


Suppose:

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

Steps:

  1. Go to CustomXIBView's nib
  2. 转到CustomXIBView的笔尖
  3. Select View (left toolbar)
  4. 选择查看(左侧工具栏)
  5. Select Show Identity Inspector (3rd option in the panel on the right)
  6. 选择Show Identity Inspector(右侧面板中的第3个选项)
  7. Specify CustomXIBView as the Custom Class of the View
    • don't do anything with CustomXIBView's File's Owner in the nib
    • 不要在笔尖中使用CustomXIBView的文件所有者做任何事情
  8. 将CustomXIBView指定为视图的自定义类,不要在笔尖中使用CustomXIBView的文件所有者执行任何操作
  9. Drag Drop Objects and connect it with CustomXIBView.h
  10. 拖放对象并将其与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/

#1


31  

As you already know, with a UIViewController we 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,我们使用-initWithNibName:bundle:方法将其与xib连接。但是......当谈到UIView时,你需要使用-loadNibNamed:owner:options:并用xib加载它。 (只是在xib中为视图指定自定义类将不起作用)


Suppose:

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

Steps:

  1. Go to CustomXIBView's nib
  2. 转到CustomXIBView的笔尖
  3. Select View (left toolbar)
  4. 选择查看(左侧工具栏)
  5. Select Show Identity Inspector (3rd option in the panel on the right)
  6. 选择Show Identity Inspector(右侧面板中的第3个选项)
  7. Specify CustomXIBView as the Custom Class of the View
    • don't do anything with CustomXIBView's File's Owner in the nib
    • 不要在笔尖中使用CustomXIBView的文件所有者做任何事情
  8. 将CustomXIBView指定为视图的自定义类,不要在笔尖中使用CustomXIBView的文件所有者执行任何操作
  9. Drag Drop Objects and connect it with CustomXIBView.h
  10. 拖放对象并将其与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/