使用Interface Builder嵌套自定义类/ XIB [英] Nesting Custom Classes/XIB's with Interface Builder

查看:102
本文介绍了使用Interface Builder嵌套自定义类/ XIB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会尽量做到尽可能短。

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

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

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.

然后,我想要获取该类,将其嵌入到其他一些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中嵌入该自定义类时,它无法识别与之关联的原始XIB,因此它要求我将IBOutlets重置为新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.

有没有人理解我想做什么并且有一个好方法?

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

推荐答案

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

Here's how I managed to make this work:

在Interface Builder中

打开外部笔尖并执行以下操作:

In Interface Builder
Open your outer nib and do this:


  • 添加UIView以定义您希望内部笔尖显示的空间。 / li>
  • 从库中添加一个UIViewController对象,将其Nib Name属性设置为内部nib文件的名称(不带扩展名)。

  • 创建IBOutlets对于外部视图控制器中的这两个项目并将它们连接起来。 (我将它们称为 innerView innerViewController 。)

  • 不要连接内部视图中定义的任何IBOutlet控制器到外部nib文件中的任何内容,只需将它们保持未连接状态。

  • Add a UIView to define the space where you want your inner nib to display.
  • Add a UIViewController object from the library, set its Nib Name property to the name of your inner nib file (without the extension).
  • Create IBOutlets for these two items in your outer view controller and wire them up. (I'll refer to them as innerView and 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.

在Xcode中

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

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

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

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

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

Build&跑!

Build & Run!

这篇关于使用Interface Builder嵌套自定义类/ XIB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆