在界面構建器中添加的自定義UIView不會加載xib

[英]Custom UIView added in the interface builder does not load the xib


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/


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/12/01/18feaf643c73ff87a48a6f7a318b02c7.html



 
  © 2014-2022 ITdaan.com 联系我们: