简体   繁体   English

“第一响应者” - 我做对了吗?

[英]“First Responder” - Did I get that right?

Let me summarize this shortly: A "First Responder" in a nib file is an object, which represents the UI control element that has the user's focus. 让我简要总结一下:nib文件中的“First Responder”是一个对象,它表示具有用户焦点的UI控件元素。 So if the user clicks on a control, the nib sets that clicked UI control as First Responder. 因此,如果用户单击控件,则nib将单击的UI控件设置为First Responder。 In my app I could make an outlet to that "First Responder" from the nib, so that I could for example send a message "make red font color" to whatever the user has activated by clicking. 在我的应用程序中,我可以从笔尖找到“First Responder”的出口,这样我就可以通过点击向用户激活的任何内容发送消息“make red font color”。

And then, if this First Responder UI control does not understand that message, the message gets passed up in the responder chain, until a parent element or grandparent (and so on) UI control element understands the message. 然后,如果此第一响应者UI控件不理解该消息,则消息将在响应者链中传递,直到父元素或祖父母(等等)UI控件元素理解该消息。 Otherwise it will be ignored. 否则它将被忽略。

So First Responder always establishes a "link" to the UI control that has focus. 因此,First Responder始终建立具有焦点的UI控件的“链接”。 Is that right? 是对的吗?

Right overall picture, wrong implementation details in the first paragraph. 正确的整体情况,第一段错误的实施细节。

A "First Responder" in a NibFile is an Object … NibFile中的“第一响应者”是一个对象......

No, actually, First Responder is nil . 不,实际上,First Responder是nil Connecting a UI control (eg, button) to First Responder in a nib is equivalent to [control setTarget:nil] in code. 将UI控件(例如,按钮)连接到nib中的First Responder相当于代码中的[control setTarget:nil]

The reason for the First Responder fake-object in the nib window is that, in IB, you set target and action at the same time (ctrl-drag to target, choose action from pop-up menu). nib窗口中First Responder假对象的原因是,在IB中,您同时设置目标和动作(按住Ctrl键拖动到目标,从弹出菜单中选择动作)。 You can't set the action and leave the target unset, like you can in code, so to set it to nil , you need to do so explicitly. 您无法设置操作并保持目标未设置,就像您可以在代码中一样,因此要将其设置为nil ,您需要明确地这样做。 That's what First Responder is for: it's a fake object representing nil , so you can set the target and action the same way you would do when setting it to a specific real target. 这就是First Responder的用途:它是一个代表nil的虚假对象,因此你可以像设置特定的真实目标一样设置目标和动作。

Of course, you can't use this to set anything else to nil , only views' targets. 当然,你不能使用此设置什么别的nil ,只有若干意见的目标。 You can only use it to mean First Responder, not anything else. 你只能用它来表示First Responder,而不是其他任何东西。

So if the user klicks on an UI control, the Nib sets … 因此,如果用户点击UI控件,Nib会设置...

The nib doesn't do anything. 笔尖没有做任何事情。 It's just a freeze-dried collection of objects stored on disk. 它只是存储在磁盘上的冻干对象集合。 Even when you instantiate NSNib, all you're doing is defrosting some objects. 即使你实例化NSNib,你所做的只是解冻一些对象。 It's the objects that do things. 这是做事的对象。

In the case at hand, when you unarchive the control you connected to First Responder from the nib, its target is set to nil (remember, that's what First Responder really is: a target of nil ). 在手头的情况下,当您取消归档从笔尖连接到First Responder的控件时,其目标设置为nil (请记住,这就是First Responder真正的目标: nil的目标)。 When a control's target is nil , and the user clicks on it, it sends its action to whichever responder is the first responder at the time. 当控件的目标nil并且用户点击它时,它会将其动作发送给当时第一个响应者。

Your second and third paragraphs are correct. 你的第二和第三段是正确的。

Your understanding is incomplete. 你的理解是不完整的。 The responder chain includes more than what we'd usually think of as "UI controls," including most importantly the current document. 响应者链包括的不仅仅是我们通常认为的“UI控件”,包括最重要的当前文档。 One of the big benefits is that it allows you to interact with the conceptually "current" whatever — current window, current text field, current document, etc — without a lot of messing around to find it. 其中一个很大的好处是,它允许您与概念上的“当前”任何东西进行交互 - 当前窗口,当前文本字段,当前文档等 - 没有太多的麻烦来找到它。

A responder is any object that will perform actions (call functions) when events (such as clicking on buttons) occur. 响应者是在发生事件(例如单击按钮)时将执行操作(调用函数)的任何对象。 The responder chain is a sequence of objects each contained in one another - for example a button inside a panel inside a window. 响应者链是一系列对象,每个对象包含在一起 - 例如窗口内面板内的按钮。 When an event occurs, we iterate through the chain until we find an object that does not have a responder set to nil and which can therefore respond to the event. 当一个事件发生时,我们遍历链,直到找到一个没有响应者设置为nil的对象,因此可以响应该事件。 So instead of providing a responder object for each button in a window, we can provide a single responder for the whole window. 因此,我们可以为整个窗口提供单个响应器,而不是为窗口中的每个按钮提供响应器对象。 The first responder is simply the first object inside the responder chain - linking an event to the first responder allows the event to pass up the chain. 第一个响应者只是响应者链中的第一个对象 - 将事件链接到第一个响应者允许事件向上传递链。

Check this link out it does a good job explaining. 检查此链接,它做得很好解释。 I think you have the gist of it: 我认为你有它的要点:

http://cocoadev.com/FirstResponder http://cocoadev.com/FirstResponder

From the source: 从来源:

The FirstResponder is the first object in the responder chain that is given the opportunity to respond to an event. FirstResponder是响应者链中的第一个对象,它有机会响应事件。

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

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