简体   繁体   English

ios Responder链-视图如何捕获事件?

[英]ios Responder chain - how does a View capture an event?

according to https://developer.apple.com/library/ios/documentation/General/Conceptual/Devpedia-CocoaApp/Responder.html , "If a view is managed by a view controller and if the view cannot handle an event, the view controller becomes the next responder" 根据https://developer.apple.com/library/ios/documentation/General/Conceptual/Devpedia-CocoaApp/Responder.html的说明 ,“如果视图是由视图控制器管理的,并且该视图无法处理事件,则视图控制器将成为下一个响应者”

What is meant by this statement? 这句话是什么意思? It is possible for a View to internally define a handler for an event and capture it intrinsically? 视图是否可以在内部为事件定义处理程序并从本质上捕获事件? If this is not the case the ViewController for the view will be called if it has explicitly defined a handler for the event? 如果不是这种情况,则为视图的ViewController如果已为事件显式定义了处理程序,则将被调用?

It seems contrary to the MVC pattern for the View to handle events - or is the documentation really saying a handler on a controller specific to the View is invoked? 似乎与View处理事件的MVC模式相反-或者文档是否真的说调用了特定于View的控制器上的处理程序?

There is a responder chain, formed by each UIResponder's nextResponder() : 有一个响应者链,由每个UIResponder的nextResponder()

  1. The UIView that we start with. 我们开始的UIView。

  2. If this UIView is a UIViewController's view , that UIViewController. 如果此UIView是UIViewController的view ,则该UIViewController。 (This is what the passage you quoted is saying.) (这就是您引用的段落所说的。)

  3. The UIView's superview. UIView的超级视图。

  4. Go back to step 2 and repeat! 返回步骤2并重复! Keep repeating until we reach... 不断重复直到我们到达...

  5. The UIWindow. UIWindow。

  6. The UIApplication. UIApplication。

  7. The UIApplication's delegate. UIApplication的委托。

Certain messages are sent, not so much to a particular object, but up the responder chain. 某些消息不是发送给特定对象,而是发送给响应者链。 We start with a particular object, but if that object doesn't have an implementation for the method, we don't crash; 我们一个特定的对象开始 ,但是如果该对象没有该方法的实现,则不会崩溃。 instead, we walk the responder chain, looking for someone further up the chain who does have an implementation for the method. 取而代之的是,我们沿着响应者链走,寻找在链中更远的某个人,该人确实对该方法有实现。 Moreover, if we never find such an implementation, we do not crash; 此外,如果我们永远也找不到这样的实现,我们不死机; the message falls off the end of the chain without penalty. 消息从链的末端掉落而不会受到惩罚。

There are two chief messages of this sort: 有两种主要信息:

  • Touch events. 触摸事件。

  • Nil-targeted actions. 无目标的动作。

As to your question, what does this have to do with MVC...? 关于您的问题,这与MVC有什么关系? Nothing. 没有。 As Josh Gafni's answer rightly says, MVC is not the only pattern in the world. 正如乔什·加夫尼(Josh Gafni)正确回答的那样,MVC并不是世界上唯一的模式。 This is about responders, not about MVC. 这是关于响应者,而不是MVC。

UIView actually already has event handlers that you can overwrite. UIView实际上已经具有可以覆盖的事件处理程序。 You can subclass UIView and handle touch events using the following methods: 您可以使用以下方法来UIView并处理触摸事件:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

There are two ways of communication between the View and View Controller. View和View Controller之间有两种通信方式。 In general, the View Controller typically tells the View what to display, and the View tends to call delegate methods when events occur. 通常,视图控制器通常会告诉视图要显示的内容,并且事件发生时,视图往往会调用委托方法。 I do understand though that you would typically expect these event handlers to be called by the View but handled in the View Controller. 我确实知道,尽管您通常希望这些事件处理程序由View调用,但在View Controller中处理。 But... 但...

  1. You get these methods for free along with a UIView and they are specifically related to the View. 您可以免费获得这些方法以及UIView ,它们与View特别相关。
  2. You can view these as lower level delegate methods that are closer to the View 您可以将它们作为更接近View的较低级别的委托方法进行查看
  3. Say you had 2 pieces of static content and any time the user touches the View the content flips. 假设您有2条静态内容,并且用户每次触摸“查看内容”翻转。 Yes, you could (and some people would say should) separate between View and View Controller, but this is such a simple bit of code and so directly related to the View that it might actually be less confusing and more concise to keep it together. 是的,您可以(有些人会说应该)在View和View Controller之间分开,但是这是一段非常简单的代码,因此与View直接相关,因此将其放在一起实际上可能不太混乱,也更加简洁。
  4. There are other design patterns than MVC, and Apple does not necessarily need to provide tools for just one design pattern solely. 除了MVC,还有其他设计模式,Apple不一定只需要为一种设计模式提供工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM