响应链在iPhone中如何工作?什么是“下一个响应者"? [英] How do Responder Chain Works in IPhone? What are the "next responders"?

查看:129
本文介绍了响应链在iPhone中如何工作?什么是“下一个响应者"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是文档所说的:

如果[事件或动作消息的]第一响应者无法处理事件或动作消息,则将其转发到称为响应者链的链接系列中的下一个响应者".响应者链允许响应者对象将处理事件或操作消息的责任转移到应用程序中的其他对象.

If the first responder [to an event or action message] cannot handle an event or action message, it forwards it to the "next responder" in a linked series called the responder chain. The responder chain allows responder objects to transfer responsibility for handling an event or action message to other objects in the application.

如果响应者链中的对象无法处理事件或操作,则会将消息重新发送给链中的下一个响应者.消息在链中向上传播,到达更高级别的对象,直到被处理为止.如果未处理,则应用程序将其丢弃.

If an object in the responder chain cannot handle the event or action, it resends the message to the next responder in the chain. The message travels up the chain, toward higher-level objects, until it is handled. If it isn't handled, the application discards it.

好的,下一个响应者是什么?

Okay, what is the next responder?

是父视图吗?背后的观点呢? iOS如何确定第一响应者和第二响应者?

Is it the parent view? The view behind it? How does iOS decide what is the first responder and second responder?

推荐答案

第一响应者"是可可中一个非常具体的概念. iOS唯一决定设置第一响应者"的时间是在文本字段获得焦点时.在所有其他时间,您必须明确控制要成为第一个响应者的对象(请参阅-canBecomeFirstResponder和-becomeFirstResponder).

The First Responder is a very specific concept in Cocoa. The only time iOS decides to set the First Responder is when a text field gets focus. At all other times you must explicitly control which object you want to be the first responder (see -canBecomeFirstResponder, -becomeFirstResponder).

没有第二响应者.

所有响应者都有一个 NextResponder (可以为nil).这意味着从任何响应者开始,都有(但可能不是)任意长度的响应者链(响应者-> nextResponder-> nextResponder->等等),沿着事件链传递事件直到处理事件为止

All responders have a NextResponder, (which can be nil). This means that starting from any responder there may be (but may not be) an arbitrarily length chain of responders (responder -> nextResponder -> nextResponder -> etc) along which events are passed until they are handled.

有一个默认链,可以是视图->超级视图->超级视图,但可能还包括 UIViewControllers,UIWindows,UIWindowControllers,UIApplication 等,因此,它在很大程度上取决于您的对象层次结构(而不​​仅仅是您的视图层次结构-因此,您不能说nextResponder始终是父视图).在OSX 10.6上,默认链对于不同类型的事件和操作甚至是不同的,甚至可以包括您的应用程序委托,它可能是响应者,也可能不是响应者,我不确定在iOS中是否是这种情况.

There is a default chain which can be view -> superview -> superview but might also include UIViewControllers, UIWindows, UIWindowControllers, UIApplication and more, so it heavily depends on your object hierarchy (not just your view hierarchy - so no, you can't say nextResponder is always the parent view). On OSX 10.6 the default chain is even different for different types of events and actions and can even include your app delegate, which may or may not be a responder, i'm not sure if this is the case in iOS tho.

默认链仅是默认值,因此,在管理了第一响应者"后,您就可以在其响应者链中插入,删除和附加项目,以实现您想要的目标.

The default chain is only the default tho, so after you have managed the First Responder it is down to you to insert, remove and append items to it's responder chain to achieve your desired aim.

响应者链非常重要且复杂,您应该花一些时间阅读有关它的Apple文档.

The responder chain is quite important and complicated, you should take time to read the Apple docs about it.

这篇关于响应链在iPhone中如何工作?什么是“下一个响应者"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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