How does UIView nextResponder know what the UIViewController is?

后端 未结 4 1825
暗喜
暗喜 2021-02-07 20:05

Just as a matter of curiosity, how does the nextResponder method implementation of the UIView class know who is the UIViewController that manages the view? The UIResponder docum

4条回答
  •  不知归路
    2021-02-07 20:16

    My guess is that the system may maintain a mapping between UIViewController objects and their root UIView objects. The code for traversal of responder chain may use this mapping to pass the event to the corresponding UIViewController object.

    Typically subviews are added using:

    subview = [viewController view]

    [superview addSubview subview]

    addSubview method automatically sets superview as the next responder for subview, so:

    a) viewController wont have the opportunity to insert itself between superview and subview.

    b) viewController does not know about superview, hence it cannot set it as its next responder.

    c) Apple recommends that a view may not be shared across controllers. In absence of multi-threading this restriction makes sense only if there is a map of views and view controllers.

提交回复
热议问题