iOS - Xcode 中的文件所有者和第一响应者是什么?

发布于 2024-09-24 14:44:39 字数 36 浏览 7 评论 0原文

iOS - Xcode 中的文件所有者和第一响应者是什么?

What are File Owner and First Responder in iOS - Xcode?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂ガ小子 2024-10-01 14:44:39

文件所有者是一个实例化的运行时对象,它在加载 nib 时拥有 nib 的内容及其出口/操作。它可以是您喜欢的任何类的实例 - 查看工具选项板的标识选项卡。

文件所有者是应用程序代码和 nib 文件内容之间的主要链接。

例如,假设您有一个 UIViewController 子类,其中包含一个 UILabel 的 IBOutlet。在界面构建器中,文件的所有者将被设置为与 UIViewController 相同的类。当您的 nib 在运行时加载时,nib 中定义的插座和操作的绑定将绑定到视图控制器的实例,因为您的视图控制器是所有者。

Nib 的加载使用:

[[NSBundle mainBundle] loadNibNamed:@"NibName" owner:nil options:nil];

owner 参数特别重要。这是一个类的运行时实例,该类拥有正在加载的笔尖的内容(出口、操作和对象)。

希望这是清楚的。要查看它的工作效果,请创建一个带有视图控制器的全新 iPhone 项目。打开 Nib 文件并查看身份选项卡。

第一响应者只是响应者链中可以响应事件的第一个对象。响应者链是可以响应事件的对象的运行时集合(或更准确地说是层次结构)。例如,假设您有一个带有视图的窗口,并且该视图上有一个文本字段。

如果该文本字段具有焦点,则它被称为链中的第一响应者。因此,如果您向第一响应者发送消息,它将首先发送到文本字段。如果文本字段无法处理该消息,它将被发送到下一个响应者。还有下一个。接下来,直到到达响应者链的末尾或某些事件消耗了事件(iirc)。

响应者链值得一读 - 点击苹果文档以获取更多信息。

The File Owner is an instantiated, runtime object that owns the contents of your nib and its outlets/actions when the nib is loaded. It can be an instance of any class you like - take a look at the identity tab of the tool palette.

File Owner is the main link between your application code and the contents of the nib file.

For example, consider you have a UIViewController subclass with an IBOutlet for a UILabel. In interface builder the File's owner will be set to the same class as your UIViewController. When your nib is loaded at runtime, the bindings of outlets and actions defined in your nib are bound to the instance of your view controller, as your view controller is the owner.

Nibs are loaded using:

[[NSBundle mainBundle] loadNibNamed:@"NibName" owner:nil options:nil];

The owner parameter is particularly important. That's the runtime instance of a class that owns the contents (outlets, actions and objects) of the nib being loaded.

Hopefully that's clear. To see this at work create a brand new iPhone project with a view controller. Open the Nib file and take a look at the identity tab.

First responder is simply the first object in the responder chain that can respond to events. The responder chain is a runtime collection (or more accurately a hierarchy) of objects that can respond to an event. For example, consider you have a window with a view and on that view is a text field.

If that text field has focus it's known as the first responder in the chain. So if you send a message to the first responder it'll be sent to the text field first. If the text field can't handle the message it'll be sent to the next responder. And the next. And the next, until you get to the end of the responder chain or something has consumed the event (iirc).

The responder chain is worth reading about - hit apple's documentation for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文