Interface Builder 是否有一个易于理解的指南(Cocoa、Xcode 等的新手)来讨论代理对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/789251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:32:54  来源:igfitidea点击:

Is there an easy to understand guide (new to Cocoa, Xcode, etc) for Interface Builder that talks about proxy objects?

xcodecocoacocoa-touchinterface-builder

提问by VesperDEM

Back in the day of C/C++, the part of the language that really hung me up was pointers. I understand them now, of course.

回到 C/C++ 时代,真正让我感到困惑的语言部分是指针。当然,我现在明白了。

Now, Interface Builder Proxy Objects or more importantly, how to use Interface Builder and what to put in the .XIB window (window that contains the File's Owner, First Responder, etc...) for a given XIB.

现在,Interface Builder 代理对象或更重要的是,如何使用 Interface Builder 以及在给定 XIB 的 .XIB 窗口(包含文件所有者、第一响应者等的窗口)中放置什么。

I've gone through quite a few examples from books and samples found both in Xcodes examples and around the web. They have been helpful, but I still feel a little lost.

我已经通过 Xcodes 示例和网络上的书籍和示例浏览了很多示例。他们很有帮助,但我仍然感到有些失落。

I feel that understanding IB is very important to understanding how to write Mac/iPhone/iPod touch applications.

我觉得理解IB对于理解如何编写Mac/iPhone/iPod touch应用程序非常重要。

I have gone through the following resources so far: Aaron Hillegass' Cocoa Programming for Mac OS X Pragmatic Programmer resources: Becoming Productive in Xcode (screencast) Cocoa Programming Coding in Objective-C 2.0 (screncast) Writing Your First iPhone Application (screencast) iPhone SDK Development

到目前为止,我已经浏览了以下资源: Aaron Hillegass 的 Mac OS X 的 Cocoa 编程 实用的程序员资源:在 Xcode 中变得高效(截屏) Cocoa Programming Coding in Objective-C 2.0 (screncast) 编写你的第一个 iPhone 应用程序(截屏) iPhone SDK开发

I've also gone over the Interface Builder Users Guide PDF from Apple.

我还阅读了 Apple 的 Interface Builder 用户指南 PDF。

Any suggested tips/resources will be appreciated!

任何建议的提示/资源将不胜感激!

回答by Jon Hess

First, placeholder is a better word than proxy here.

首先,占位符在这里比代理更好。

Normally when you have an object in a NIB/XIB file it means that loading the NIB file will create that instance. The placeholder objects are objects that will already exist when the NIB file is loaded, and they appear inside of the NIB so that you can make connections between the objects that will be created by loading the NIB and the objects that already exist.

通常,当您在 NIB/XIB 文件中有一个对象时,这意味着加载 NIB 文件将创建该实例。占位符对象是在加载 NIB 文件时已经存在的对象,它们出现在 NIB 内部,以便您可以在通过加载 NIB 创建的对象和已经存在的对象之间建立连接。

The file's owner, first responder and application are all placeholders.

文件的所有者、第一响应者和应用程序都是占位符。

The file's owner is placeholder for the object that will load the nib. All of the NIB loading methods take an 'owner' parameter. When you make a connection with the File's owner, when it's established at runtime, it will be connected to the owner object passed in to the nib loading method. Many UIKit and AppKit classes invoke the nib loading methods for you. NSApplication, NSViewController, NSWindowController, UIApplication, and UIViewControllerall load NIB files on your behalf. When they do that they pass self as the owner parameter to the nib loading methods. That's why when you use a view controller or a window controller you set the file's owner to your subclass and make most of the connections between your views and the file's owner.

文件的所有者是将加载笔尖的对象的占位符。所有 NIB 加载方法都采用“所有者”参数。当您与 File 的所有者建立连接时,当它在运行时建立时,它将连接到传递给 nib 加载方法的所有者对象。许多 UIKit 和 AppKit 类会为您调用 nib 加载方法。NSApplicationNSViewControllerNSWindowControllerUIApplicationUIViewController所有代表您加载 NIB 文件。当他们这样做时,他们将 self 作为所有者参数传递给 nib 加载方法。这就是为什么当您使用视图控制器或窗口控制器时,您将文件的所有者设置为您的子类,并在您的视图和文件所有者之间建立大部分连接。

The NSApplicationinstance is a simple placeholder for [NSApplication sharedApplication]. That's a global singleton and the icon in Interface Builder represents that global singleton. Loading the NIB file does not create a second NSApplicationinstance. By contrast, when a NIB file contains a window, if you load it a dozen times, you'll have a dozen window instances, but still one NSApplicationinstance.

NSApplication实例是 的一个简单占位符[NSApplication sharedApplication]。这是一个全局单例,Interface Builder 中的图标代表该全局单例。加载 NIB 文件不会创建第二个NSApplication实例。相比之下,当一个 NIB 文件包含一个窗口时,如果你加载它十几次,你将有十几个窗口实例,但仍然是一个NSApplication实例。

The first responder is unique. Connecting an action to the first responder means that when the action is fired, it should dynamically be sent to the responder chain. The responder chain typically starts with the focused view, and continues up through the view hierarchy and includes some controllers and delegates. Each object in the chain gets a shot at handling the action. Menu items work great with the responder chain. If you had a menu item for "Make Bold" that is supposed to make the currently selected text bold, you might start by hooking that up to an NSApplicationsubclass, but then you'd have to know all of the situations that "Make Bold" applies, and how to handle them. A text view and an editable web view would probably need different code to handle "make bold" and bottling this all up in one object would get quite complex and wouldn't be very extensible. Instead you could connect the "Make Bold" menu item's action up to a makeBold:action on the First Responder. This would mean that when the menu item was selected, the focused object, or one of its parents that responded to makeBold:, would get the makeBold:message. Now many classes can implement a makeBold:method and respond to this menu item when they're in focus.

第一响应者是独一无二的。将动作连接到第一响应者意味着当动作被触发时,它应该动态地发送到响应者链。响应者链通常从焦点视图开始,并继续通过视图层次结构并包括一些控制器和委托。链中的每个对象都有机会处理动作。菜单项与响应者链配合得很好。如果您有一个“Make Bold”菜单项应该使当前选定的文本变粗,您可以先将它连接到一个NSApplication子类,但是您必须知道“大胆”适用的所有情况,以及如何处理它们。一个文本视图和一个可编辑的 web 视图可能需要不同的代码来处理“加粗”,并且将所有这些都装在一个对象中会变得非常复杂并且不会非常可扩展。相反,您可以将“Make Bold”菜单项的操作连接到makeBold:对 First Responder 的操作。This would mean that when the menu item was selected, the focused object, or one of its parents that responded to makeBold:, would get the makeBold:message. 现在,许多类都可以实现一个makeBold:方法并在它们处于焦点时响应此菜单项。

回答by Nicholas Riley

With the exception of "File's Owner" (which gets hooked up when the nib/xib is loaded), the objects you create in IB are real objects, not proxy objects.

除了“文件的所有者”(在加载 nib/xib 时会被连接)之外,您在 IB 中创建的对象是真实对象,而不是代理对象。

You may want to look through some existing example code to see how IB hooks things up. Is there anything in the documentation/tutorials you've read that confuses you? It's hard to do much with "a little lost".

您可能想要查看一些现有的示例代码,以了解 IB 如何将事情联系起来。您阅读过的文档/教程中是否有任何让您感到困惑的内容?“有点迷失”很难做很多事情。

回答by Dave Gallagher

Apple has the following doc which explains the File's Owner, First Responderand Applicationplaceholder objects found in Interface Builder NIB/XIB files here:

Apple 有以下文档,其中解释了在 Interface Builder NIB/XIB 文件中找到的File's OwnerFirst ResponderApplication占位符对象:

Interface Builder User Guide: Placeholder Objects

Interface Builder 用户指南:占位符对象

I realized in your question you mentioned reading Apple's Interface Builder docs, but I felt it's appropriate posting this here for future reference. Also, the docs have been updated since your question was first asked, so it's possible they made things more clear since then.

我在您的问题中意识到您提到阅读 Apple 的 Interface Builder 文档,但我认为将其发布在这里以供将来参考是合适的。此外,自从您第一次提出问题以来,文档已经更新,因此从那时起他们可能使事情变得更加清晰。