使用Interface Builder嵌套自定义类/ XIB

时间:2021-07-21 05:15:23

I'll try to make this as short as I can.

我会尽量做到尽可能短。

I wrote a custom class that extends UIView with a couple of IBOutlet properties, and it has a XIB associated with it where those IBOutlets are linked to.

我编写了一个自定义类,它使用一些IBOutlet属性扩展了UIView,并且它与这些IBOutlet链接的XIB相关联。

I then want to take that class, embed it in some other XIB (for example, a table cell), and just have it work.

然后我想把这个类,嵌入到其他一些XIB(例如,一个表格单元格)中,然后让它工作。

It seems that when I embed that custom class in the new XIB, it does not recognize the original XIB I associated with it, so it asks for me to reset the IBOutlets to interface elements on the new XIB. This is lame.

似乎当我在新的XIB中嵌入该自定义类时,它无法识别与之关联的原始XIB,因此它要求我将IBOutlet重置为新XIB上的接口元素。这太蹩脚了。

Does anyone understand what I am trying to do and have a good approach?

有谁知道我想做什么并有一个好的方法?

2 个解决方案

#1


8  

Here's how I managed to make this work:

以下是我设法完成这项工作的方法:

In Interface Builder
Open your outer nib and do this:

在Interface Builder中打开外部笔尖并执行以下操作:

  • Add a UIView to define the space where you want your inner nib to display.
  • 添加UIView以定义您希望内部笔尖显示的空间。

  • Add a UIViewController object from the library, set its Nib Name property to the name of your inner nib file (without the extension).
  • 从库中添加UIViewController对象,将其Nib Name属性设置为内部nib文件的名称(不带扩展名)。

  • Create IBOutlets for these two items in your outer view controller and wire them up. (I'll refer to them as innerView and innerViewController.)
  • 在外部视图控制器中为这两个项创建IBOutlet并将它们连接起来。 (我将它们称为innerView和innerViewController。)

  • Do not connect any of the IBOutlets defined in your inner view controller to anything in your outer nib file, just leave them unconnected.
  • 不要将内部视图控制器中定义的任何IBOutlet连接到外部nib文件中的任何内容,只需将它们保持未连接状态即可。

In Xcode
In your outer view controller's viewDidLoad method, add these two lines:

在Xcode中在外部视图控制器的viewDidLoad方法中,添加以下两行:

[self.innerView addSubview:self.innerViewController.view];
self.innerViewController.view.frame = CGRectMake(0, 0, self.innerView.frame.size.width, self.innerView.frame.size.height);

If your outer nib is a custom UITableViewCell, put those lines in your awakeFromNib method instead.

如果您的外部笔尖是自定义UITableViewCell,请将这些行放在awakeFromNib方法中。

Build & Run!

建立并运行!

#2


1  

I assume you're simply putting UIViews in a nib for use by a UIViewController that's purely in code. Apple calls this a detached nib file.

我假设你只是简单地将UIViews放在一个笔尖*UIViewController使用,它纯粹是在代码中。 Apple将其称为分离的nib文件。

Follow the guide I linked to for details and example of how to get this to work.

按照我链接的指南了解详细信息以及如何使其工作的示例。

Regarding embedding a view inside another in Interface Builder, you need to add a UIView element from the Library into the parent view, and set its class in the Inspector. Once the class of the embedded view is set, your IBOutlets should be visible.

关于在Interface Builder中将视图嵌入到另一个视图中,需要将库中的UIView元素添加到父视图中,并在Inspector中设置其类。设置嵌入视图的类后,您的IBOutlet应该是可见的。

#1


8  

Here's how I managed to make this work:

以下是我设法完成这项工作的方法:

In Interface Builder
Open your outer nib and do this:

在Interface Builder中打开外部笔尖并执行以下操作:

  • Add a UIView to define the space where you want your inner nib to display.
  • 添加UIView以定义您希望内部笔尖显示的空间。

  • Add a UIViewController object from the library, set its Nib Name property to the name of your inner nib file (without the extension).
  • 从库中添加UIViewController对象,将其Nib Name属性设置为内部nib文件的名称(不带扩展名)。

  • Create IBOutlets for these two items in your outer view controller and wire them up. (I'll refer to them as innerView and innerViewController.)
  • 在外部视图控制器中为这两个项创建IBOutlet并将它们连接起来。 (我将它们称为innerView和innerViewController。)

  • Do not connect any of the IBOutlets defined in your inner view controller to anything in your outer nib file, just leave them unconnected.
  • 不要将内部视图控制器中定义的任何IBOutlet连接到外部nib文件中的任何内容,只需将它们保持未连接状态即可。

In Xcode
In your outer view controller's viewDidLoad method, add these two lines:

在Xcode中在外部视图控制器的viewDidLoad方法中,添加以下两行:

[self.innerView addSubview:self.innerViewController.view];
self.innerViewController.view.frame = CGRectMake(0, 0, self.innerView.frame.size.width, self.innerView.frame.size.height);

If your outer nib is a custom UITableViewCell, put those lines in your awakeFromNib method instead.

如果您的外部笔尖是自定义UITableViewCell,请将这些行放在awakeFromNib方法中。

Build & Run!

建立并运行!

#2


1  

I assume you're simply putting UIViews in a nib for use by a UIViewController that's purely in code. Apple calls this a detached nib file.

我假设你只是简单地将UIViews放在一个笔尖*UIViewController使用,它纯粹是在代码中。 Apple将其称为分离的nib文件。

Follow the guide I linked to for details and example of how to get this to work.

按照我链接的指南了解详细信息以及如何使其工作的示例。

Regarding embedding a view inside another in Interface Builder, you need to add a UIView element from the Library into the parent view, and set its class in the Inspector. Once the class of the embedded view is set, your IBOutlets should be visible.

关于在Interface Builder中将视图嵌入到另一个视图中,需要将库中的UIView元素添加到父视图中,并在Inspector中设置其类。设置嵌入视图的类后,您的IBOutlet应该是可见的。