super respondsToSelector:返回 true 但实际上调用 super (selector) 会给出“发送到实例的无法识别的选择器"; [英] super respondsToSelector: returns true but actually calling super (selector) gives "unrecognized selector sent to instance"

查看:16
本文介绍了super respondsToSelector:返回 true 但实际上调用 super (selector) 会给出“发送到实例的无法识别的选择器";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有点困惑.

我有一个 UIScrollView 的子类,这是我尝试像 UI 元素一样水平滚动的表格视图".UIScrollView 本身设置了它在内部使用的 UIGestureRecognizers,并且它似乎将自己设置为那些 UIGestureRecognizers 的委托.我在我的水平表格元素/单元格上也有我自己的 UIGestureRecognizer 设置,我自己的类设置为我自己的 UIGestureRecognizer 的委托.由于我的类是 UIScrollView 的子类,因此在运行时,UIGestureRecognizer 委托调用会针对 UIScrollView 内置 UIGestureRecognizers 和我自己的 UIGestureRecognizers 进入我的类.有点像 PITA,但我们可以通过传递我们不关心的那些来解决这个问题.

I have a subclass of UIScrollView, which is my attempt at a horizontally scrolling "table view" like UI element. UIScrollView itself sets up UIGestureRecognizers it uses internally, and it appears to set itself up as the delegate for those UIGestureRecognizers. I also have my own UIGestureRecognizer setup on my horizontal table elements/cells and my own class set as delegate for my own UIGestureRecognizer. Since my class is a subclass of UIScrollView, at runtime, the UIGestureRecognizer delegate calls come to my class for both the UIScrollView in-built UIGestureRecognizers and my own UIGestureRecognizers. A bit of a PITA but we can work around this by passing on the ones we don't care about.

-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 
{ 
   if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]])
      return NO; 
      else
      {  
        if ([super respondsToSelector:@selector(gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:)])
           return [super gestureRecognizer:gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:otherGestureRecognizer];
           else 
           return NO;
        }
}

问题是检查 [super respondsToSelector:@selector()] 返回 YES,但是当我实际调用它时 return [super gestureRecognizer:gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:otherGestureRecognizer]; 我得到以下异常

The problem is that the check [super respondsToSelector:@selector()] returns YES, but when I then actually call it return [super gestureRecognizer:gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:otherGestureRecognizer]; I get the following exception

2012-08-31 12:02:06.156 MyApp[35875:707] -[MyAppHorizo​​ntalImageScroller gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:]:无法识别的选择器发送到实例 0x21dd50

2012-08-31 12:02:06.156 MyApp[35875:707] -[MyAppHorizontalImageScroller gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:]: unrecognized selector sent to instance 0x21dd50

我以为它应该显示

-[UIScrollView 手势识别器:shouldRecognizeSimultaneouslyWithGestureRecognizer:]

-[UIScrollView gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:]

但这可能没问题.但问题是它说它会响应然后没有响应.

But that may be OK. But the problem is that it says that it responds and then doesn't.

另外两个 UIGestureRecognizer 委托例程使用此代码(显然不同的选择器).

The other two UIGestureRecognizer delegate routines work with this code (different selectors obviously).

感谢您的任何见解.

推荐答案

除非您在类中覆盖响应选择器,否则您将在调用 super 时使用默认实现,该实现将检查当前实例.如果你想查看某类对象的实例是否响应选择器,请使用 +(BOOL)instancesRespondToSelector:(SEL)aSelector;

Unless you override responds to selector in your class you will be using the default implementation when you call super which will check the current instance. If you want to see if a instance of a type of object responds to a selector use +(BOOL)instancesRespondToSelector:(SEL)aSelector;

这将检查对象及其父对象.因此,在您的情况下,您需要以下内容:

This will check the object and its parent objects. So in your case you want to the following:

[UIScrollView instancesRespondToSelector:@selector(gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:)]

这篇关于super respondsToSelector:返回 true 但实际上调用 super (selector) 会给出“发送到实例的无法识别的选择器";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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