将事件传递给响应者链中的下一个响应者的诀窍是什么? [英] What's the trick to pass an event to the next responder in the responder chain?

查看:152
本文介绍了将事件传递给响应者链中的下一个响应者的诀窍是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apple非常有趣。我的意思是,他们说这有效:

   - (void)touchesBegan:(NSSet *)触及withEvent:(UIEvent *) event {
UITouch * touch = [touches anyObject];
NSUInteger numTaps = [touch tapCount];
if(numTaps< 2){
[self.nextResponder touchesBegan:touches withEvent:event];
} else {
[self handleDoubleTap:touch];
}
}

我有一个View Controller。如您所知,View Controllers继承自UIResponder。 View Controller创建一个继承自UIView的MyView对象,并将其作为子视图添加到它自己的视图中。



所以我们有:



View Controller>有一个View(自动)>有一个MyView(这是一个UIView)。



现在在MyView中我放了那个代码像上面的NSLog打印触摸MyView。但我将事件转发给下一个响应者,就像上面一样。在ViewController中我有另一个touchesBegan方法,它只打印一个NSLog和一个触摸视图控制器。



现在猜猜:当我触摸MyView时,它打印出来触摸MyView。当我触摸MyView的外部时,这是VC的视图,然后我得到一个触摸的视图控制器。所以两个都有效!但是什么不起作用是转发事件。因为现在,实际上下一个响应者应该是视图控制器,因为中间没有别的东西。但是当我转发它时,VC的事件处理方法永远不会被调用。



%$&!§!!



想法?



想出奇怪的东西
MyView的下一个响应者是视图控制器的视图。这是有道理的,因为MyView是一个子视图。但是我没有从视图控制器修改这个UIView。这没什么习惯的。它没有实现任何触摸事件处理。消息是否应该传递给视图控制器?我怎么能让它通过?如果我在MyView中删除事件处理代码,那么事件很好地到达视图控制器。

解决方案

根据类似问题,您的方法应该有效。



这让我认为你的观点的 nextResponder 而不是实际上是 ViewController ,你怀疑。



我会添加一个快速 NSLog 在您的转发代码中检查您的 nextResponder 真正指向的内容:

  if(numTaps< 2){
NSLog(@nextResponder =%@,self.nextResponder);
[self.nextResponder touchesBegan:touches withEvent:event];
}

您还可以更改其他NSLog消息,以便输出类型和地址信息:

  NSLog(@touch%@,self); 




已触及< UIView 0x12345678>


这可以帮助您开始诊断问题。


Apple is really funny. I mean, they say that this works:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch* touch = [touches anyObject];
    NSUInteger numTaps = [touch tapCount];
    if (numTaps < 2) {
        [self.nextResponder touchesBegan:touches withEvent:event];
   } else {
        [self handleDoubleTap:touch];
   }
}

I have a View Controller. Like you know, View Controllers inherit from UIResponder. That View Controller creates a MyView object that inherits from UIView, and adds it as a subview to it's own view.

So we have:

View Controller > has a View (automatically) > has a MyView (which is a UIView).

Now inside of MyView I put that code like above with a NSLog that prints "touched MyView". But I forward the event to the next responder, just like above. And inside the ViewController I have another touchesBegan method that just prints an NSLog a la "touched view controller".

Now guess what: When I touch the MyView, it prints "touched MyView". When I touch outside of MyView, which is the view of the VC then, I get a "touched view controller". So both work! But what doesn't work is forwarding the event. Because now, actually the next responder should be the view controller, since there's nothing else inbetween. But the event handling method of the VC is never called when I forward it.

%$&!§!!

Ideas?

Figured out weird stuff MyView's next responder is the view of the view controller. That makes sense, because MyView is a subview of that. But I didn't modify this UIView from the view controller. it's nothing custom. And it doesn't implement any touch event handling. Shouldn't the message get passed on to the view controller? How could I just let it pass? If I remove the event handling code in MyView, then the event arrives nicely in the view controller.

解决方案

According to a similar question, your method should work.

This leads me to think that your view's nextResponder is not actually the ViewController, as you suspect.

I would add a quick NSLog in your forwarding code to check what your nextResponder really points to:

if (numTaps < 2) {
    NSLog(@"nextResponder = %@", self.nextResponder);
    [self.nextResponder touchesBegan:touches withEvent:event];
}

You can also change your other NSLog messages so that they output type and address information:

NSLog(@"touched %@", self);

touched <UIView 0x12345678>

This should get you started on diagnosing the problem.

这篇关于将事件传递给响应者链中的下一个响应者的诀窍是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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