响应链在 iPhone 中如何工作? “下一个响应者”是什么?

发布于 2024-11-25 02:35:21 字数 311 浏览 1 评论 0原文

文档是这样说的:

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

如果响应者链中的某个对象无法处理该事件或操作,它会将消息重新发送给链中的下一个响应者。消息沿着链向上传输,到达更高级别的对象,直到被处理。如果不处理,应用程序将丢弃它。

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

这是父视图吗?背后的风景? iOS 如何决定第一响应者和第二响应者?

This is what the documentation says:

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

栀子花开つ 2024-12-02 02:35:21

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

不存在第二响应者这样的东西。

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

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

默认链只是默认的,因此在管理第一响应者之后,您可以向其响应者链插入、删除和附加项目以实现您的预​​期目标。

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

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

There is no such thing as a second responder.

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.

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.

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

空名 2024-12-02 02:35:21

来自 nextResponder

UIResponder 类不存储或设置下一个响应者
自动,而不是默认返回 nil。子类必须
覆盖此方法以设置下一个响应者。 UIView 实现了这个
方法通过返回管理它的 UIViewController 对象(如果它
有一个)或其超级视图(如果没有); UIViewController 实现
该方法通过返回其视图的超级视图; UIWindow 返回
application 对象,UIApplication 返回 nil。

From the docs for the nextResponder:

The UIResponder class does not store or set the next responder
automatically, instead returning nil by default. Subclasses must
override this method to set the next responder. UIView implements this
method by returning the UIViewController object that manages it (if it
has one) or its superview (if it doesn’t); UIViewController implements
the method by returning its view’s superview; UIWindow returns the
application object, and UIApplication returns nil.

冬天旳寂寞 2024-12-02 02:35:21

应用程序使用响应者对象接收和处理事件。

响应者对象是 UIResponder 类的任何实例,

  • 常见子类包括

    UIView、UIViewController 和 UIApplication

响应者接收原始事件数据,并且必须处理该事件或将其转发到另一个响应者对象。

当您的应用程序收到事件时,UIKit 会自动将该事件定向到

  • 最合适的响应者对象,称为

    第一响应者。

未处理事件在活动响应者链中从响应者传递到响应者,

这是应用程序响应者对象的动态配置。

现在看下面的屏幕截图,同时考虑前面的视图层次结构:

img

UIbutton/UITextField --(nextResponder)-> UIView --(nextResponder)-> > UIViewController

--(nextResponder)->; UIWindow --(nextResponder)-> UIApplication --(nextResponder)-> UIApplicationDelegate

这就是Responder链在iOS上的工作方式,希望它能对任何人有所帮助另外Apple网站上的最新文章是--> 链接(解释得很好。)

Apps receive and handle events using responder objects.

A responder object is any instance of the UIResponder class,

  • common subclasses include

    UIView, UIViewController, and UIApplication.

Responders receive the raw event data and must either handle the event or forward ???????? it to another responder object.

When your app receives an event, UIKit automatically directs that event to the

  • most appropriate responder object, known as the

    first responder.

Unhandled events are passed from responder to responder in the active responder chain,

which is the dynamic configuration of your app’s responder objects.

Now look at the below screen-shot, Also consider the View-Hierarchies from the front:

img

UIbutton/UITextField --(nextResponder)-> UIView --(nextResponder)-> UIViewController

--(nextResponder)-> UIWindow --(nextResponder)-> UIApplication --(nextResponder)-> UIApplicationDelegate

That is how the Responder chain works on iOS, hope it will help anyone Also the latest article on Apple's website is --> Link (Very Well explained.)

原来分手还会想你 2024-12-02 02:35:21

任何事件的响应者链都是

UIView ->视图控制器->窗口-> App Delegate

运行以下代码以更好地理解。

//
//  AppDelegate.swift
//  ResponderChain
//
//  Created by Ankit on 02/09/17.
//  Copyright © 2017 Ankit. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

   override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("App Delegate touch began")
    }


}



//
//  ViewController.swift
//  ResponderChain
//
//  Created by Ankit on 02/09/17.
//  Copyright © 2017 Ankit. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("ViewController touch Began")
        next?.touchesBegan(touches, with: event)
    }


}

extension UIWindow{
    open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("Window Touch Began")
        next?.touchesBegan(touches, with: event)
    }
}
extension UIView{
    open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("UIView touch Began")
        next?.touchesBegan(touches, with: event)
    }
}

The responder chain for any event is

UIView -> ViewController -> Window-> App Delegate

Run the below code for better understanding.

//
//  AppDelegate.swift
//  ResponderChain
//
//  Created by Ankit on 02/09/17.
//  Copyright © 2017 Ankit. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

   override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("App Delegate touch began")
    }


}



//
//  ViewController.swift
//  ResponderChain
//
//  Created by Ankit on 02/09/17.
//  Copyright © 2017 Ankit. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("ViewController touch Began")
        next?.touchesBegan(touches, with: event)
    }


}

extension UIWindow{
    open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("Window Touch Began")
        next?.touchesBegan(touches, with: event)
    }
}
extension UIView{
    open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("UIView touch Began")
        next?.touchesBegan(touches, with: event)
    }
}
梦年海沫深 2024-12-02 02:35:21

响应者链是一系列链接的响应者对象。它从第一个响应者开始,以应用程序对象结束。如果第一个响应者无法处理事件,它会将事件转发给响应者链中的下一个响应者。

响应者对象是可以响应和处理事件的对象。 UIResponder 类是所有响应程序对象的基类,它不仅定义了事件处理的编程接口,还定义了常见响应程序行为的编程接口。 UIApplication、UIViewController 和 UIView 类的实例是响应者,这意味着所有视图和大多数关键控制器对象都是响应者。请注意,核心动画层不是响应者。

第一响应者被指定为首先接收事件。通常,第一响应者是视图对象。对象通过执行两件事成为第一响应者:

Overriding the canBecomeFirstResponder method to return YES.

Receiving a becomeFirstResponder message. If necessary, an object can send itself this message.

请参阅 Apple 文档以获取更多说明。

The responder chain is a series of linked responder objects. It starts with the first responder and end with the app object. If the first responder cannot handle an event, it forwards the event to the next responder in the responder chain.

A responder object is an object that can respond to and handle events. The UIResponder class is the base class for all responder objects, and it defines the programmatic interface not only for event handling but also for common responder behavior. Instances of the UIApplication, UIViewController, and UIView classes are responders, which means that all views and most key controller objects are responders. Note that Core Animation layers are not responders.

The first responder is designated to receive events first. Typically, the first responder is a view object. An object becomes the first responder by doing two things:

Overriding the canBecomeFirstResponder method to return YES.

Receiving a becomeFirstResponder message. If necessary, an object can send itself this message.

refer Apple doc for more explanation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文