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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3768602/
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-08-30 17:46:33  来源:igfitidea点击:

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

iosiphoneinterface-builderfirst-responder

提问by Matrix

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

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

回答by dannywartnaby

The File Owner is an instantiated, runtimeobject 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.

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

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.

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

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.

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

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.

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

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).

如果该文本字段具有焦点,则它被称为链中的第一响应者。因此,如果您向第一响应者发送消息,它将首先发送到文本字段。如果文本字段无法处理消息,它将被发送到下一个响应者。接下来。而接下来,直到你得到响应链的末端或事情已经消耗的事件(IIRC)。

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

响应者链值得一读 - 点击苹果的文档了解更多信息。