“第一响应者"- 我做对了吗? [英] "First Responder" - Did I get that right?

查看:16
本文介绍了“第一响应者"- 我做对了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我简短地总结一下:nib 文件中的第一响应者"是一个对象,它表示具有用户焦点的 UI 控件元素.因此,如果用户单击控件,则笔尖会将单击的 UI 控件设置为第一响应者.在我的应用程序中,我可以从笔尖为第一响应者"创建一个出口,例如,我可以向用户通过点击激活的任何内容发送一条消息使红色字体颜色".

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. So if the user clicks on a control, the nib sets that clicked UI control as 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 UI 控件不理解该消息,则该消息将在响应者链中传递,直到父元素或祖父(等等)UI 控件元素理解该消息.否则会被忽略.

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. Otherwise it will be ignored.

所以 First Responder 总是建立一个指向具有焦点的 UI 控件的链接".是吗?

So First Responder always establishes a "link" to the UI control that has focus. Is that right?

推荐答案

整体图正确,第一段实现细节错误.

Right overall picture, wrong implementation details in the first paragraph.

NibFile 中的第一响应者"是一个对象……

A "First Responder" in a NibFile is an Object …

不,实际上,First Responder 是 nil.将 UI 控件(例如按钮)连接到笔尖中的 First Responder 相当于代码中的 [control setTarget:nil].

No, actually, First Responder is nil. Connecting a UI control (e.g., button) to First Responder in a nib is equivalent to [control setTarget:nil] in code.

nib 窗口中出现 First Responder fake-object 的原因是,在 IB 中,您同时设置了目标和动作(ctrl-drag 到目标,从弹出菜单中选择动作).您不能像在代码中一样设置操作并保持目标未设置,因此要将其设置为 nil,您需要明确地这样做.这就是 First Responder 的用途:它是一个表示 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). 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. 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.

当然,您不能使用它来将其他任何内容设置为 nil,只能设置视图的目标.您只能用它来表示 First Responder,而不能表示其他任何东西.

Of course, you can't use this to set anything else to nil, only views' targets. You can only use it to mean First Responder, not anything else.

因此,如果用户点击 UI 控件,Nib 将设置……

So if the user klicks on an UI control, the Nib sets …

笔尖没有任何作用.它只是存储在磁盘上的对象的冻干集合.即使您实例化 NSNib,您所做的也只是解冻一些对象.是对象做事.

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. It's the objects that do things.

在手头的例子中,当你从笔尖解压连接到 First Responder 的控件时,它的目标被设置为 nil(记住,这就是 First Responder 的真正含义:nil代码>无).当控件的目标是 nil 并且用户点击它时,它会将其动作发送给当时的第一响应者.

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

你的第二和第三段是正确的.

Your second and third paragraphs are correct.

这篇关于“第一响应者"- 我做对了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆