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

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

问题描述

让我简要总结一下:nib文件中的First Responder是一个对象,它表示用户关注的UI控件元素。因此,如果用户单击控件,则nib将单击UI控件的nib设置为First Responder。在我的应用程序中,我可以从笔记本电脑的第一响应者出口,以便我可以例如发送消息使红色字体颜色的用户已通过单击激活。



然后,如果这个First Responder UI控件不能理解消息,那么消息会在响应器链中传递,直到父元素或祖父(等等)UI控件元素理解消息。否则会被忽略。



所以首先响应者总是建立一个链接到具有焦点的UI控件。是正确的吗?

解决方案

第一段中错误的实施细节。


NibFile中的First Responder是一个对象...


实际上,第一响应者是 nil 。将UI控件(例如,按钮)连接到nib中的第一响应者相当于代码中的 [control setTarget:nil]



在nib窗口中第一个响应者伪造对象的原因是,在IB中,您同时设置目标和动作(ctrl-拖动到目标,从弹出菜单中选择动作)。你不能像在代码中那样设置动作并退出目标,所以要设置为 nil ,你需要明确这样做。这是First Responder的用途:它是一个表示 nil 的假对象,所以你可以像设置一个特定的真实目标一样设置目标和动作。



当然,您不能使用此选项为 nil 设置任何其他值,只能查看目标。


因此,如果用户在UI控件上点击,Nib会设置...


nib不做任何事情。它只是一个冻结的对象存储在磁盘上的集合。即使你实例化NSNib,你所做的一切是除霜一些对象。



在这种情况下,当您取消归档从nib连接到First Responder的控件时,其目标设置为 nil (记住,这就是First Responder的真正目的: nil )。当控件的目标是 nil ,并且用户点击它时,它会将其操作发送给当时第一个响应者。



您的第二和第三段是正确的。


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.

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.

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.

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

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.

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.

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.

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

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.

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天全站免登陆