简体   繁体   English

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

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

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

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. 它可以是您喜欢的任何类的实例-查看工具选项板的Identity选项卡。

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. 例如,假设您有一个带有UILabel的IBOutlet的UIViewController子类。 In interface builder the File's owner will be set to the same class as your UIViewController. 在界面生成器中,文件的所有者将设置为与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. owner参数特别重要。 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. 为了在工作中看到这一点,请创建一个带有视图控制器的全新iPhone项目。 Open the Nib file and take a look at the identity tab. 打开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. 响应者链值得一读-有关更多信息,请访问Apple的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM