ios - Storyboard中的外部对象

标签 ios objective-c storyboard xib nsbundle

Apple 已经有一段时间为我们提供 Storyboard了,我从一开始就关注它的演变,它有一些优点和缺点,但如果你将它与老式的“xib”进行比较,那肯定是一个“ future ” "文件...

我在这里所知道的是一种叫做“外部对象”的东西。使用标准“xibs”时,我们总是能够从 IB 对象库(右侧 Pane )中拖放两种特殊类型的对象

“文件所有者”是这个“外部对象”的一个例子。

我们能够将另一个 ExternalObject 拖放到我们的 xib 文件中,并通过使用此 UINib API 实例化此类 ViewController 将其连接到某个外部对象:

- (NSArray *)instantiateWithOwner:(id)ownerOrNil options:(NSDictionary *)optionsOrNil;

... 是的,这是可能的,文档:

If the nib file contains any proxy objects beyond just the File’s Owner proxy object, you can specify the runtime replacement objects for those proxies using the options dictionary. In that dictionary, add the UINibExternalObjects key and set its value to a dictionary containing the names of any proxy objects (the keys) and the real objects to use in their place. The proxy object’s name is the string you assign to it in the Name field of the Interface Builder inspector window.

现在说到 Storyboard - 外部对象在对象检查器中不可用。这是有道理的,因为我还没有找到任何允许我们将外部对象注入(inject)到从 Storyboard创建的 View Controller 中的 API。我们只有这个:

- (id)instantiateInitialViewController;

- (id)instantiateViewControllerWithIdentifier:(NSString *)identifier;

所以没有办法传递我们的“选项”。

然而...

外部对象在 Storyboard中不可用,直到...您创建一个 Swift 项目(也许还有另一个因素在我的 Storyboard中启用外部对象,我不确定)。

因此可以设置 Storyboard,让我们可以将外部对象拖放到 Storyboard中。但是我们如何才能利用这一点呢?如何从 Storyboard加载 View Controller 并连接这些代理对象?

最佳答案

您可以将 NSObject 拖放到 View Controller 场景中,并在检查器中为其指定正确的类。

enter image description here

可以按预期接线

enter image description here

我为 this question/answer 创建了一个示例项目和 published it at GitHub .

关于ios - Storyboard中的外部对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28098445/

相关文章:

ios - iOS 中的 MVVM : who is the responsible for the assembly?

objective-c - 在同时使用Swift代码的Swift项目中使用Objective-C代码

linker - CATiledLayer 无法处理我的 iOS5 项目

ios - Sprite 显示为黑色而不是透明

javascript - 如何通过 JavaScript/jQuery(或 PHP)检测 iPhone 6 和 6 Plus?

ios - 运行模拟器时偶尔会出现引用 Gatekeeper 的日志消息

objective-c - 获取 nil 对象的类类型?

iOS 7 应用程序加载 View Flash 出现之前

Storyboard显示为xml文档。如何将 View 切换回默认图形模式?

ios - Storyboard中的自定义 UIView 不显示 subview