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

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

问题描述

我有点困惑。



我有一个UIScrollView的子类,这是一个像UI元素的水平滚动表视图的尝试。 UIScrollView本身设置它在内部使用的UIGestureRecognizer,它似乎设置为这些UIGestureRecognizer的委托。我也有自己的UIGestureRecognizer设置在我的水平表格元素/单元格和我自己的类设置为我自己的UIGestureRecognizer的委托。由于我的课程是UIScrollView的子类,因此在运行时,UIGestureRecognizer委托调用来到UIScrollView内建的UIGestureRecognizer和我自己的UIGestureRecognizer的类。有一点PITA,但我们可以通过传递我们不在乎的那些来解决这个问题。

   - (BOOL手势识别器(UIGestureRecognizer *)手势识别器应该识别同时使用GestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 
{
if([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]])
return NO;
else
{
if([super respondToSelector:@selector(gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer :)]
return [super gestureRecognizer:gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:otherGestureRecognizer];
else
return NO;
}
}

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


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


我本以为应该显示


- [UIScrollView gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:]


但这可以。但是问题是它表示它响应然后没有。



另外两个UIGestureRecognizer代理程序使用这个代码(显然是不同的选择器)。 >

感谢任何见解。

解决方案

除非你在你的当您调用super将检查当前实例时,您将使用默认实现。如果您想查看一个对象类型的实例是否响应选择器使用 +(BOOL)instancesRespondToSelector:(SEL)aSelector;



这将检查对象及其父对象。所以在你的情况下,你想要如下:

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


OK, I am a little confused.

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;
        }
}

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] -[MyAppHorizontalImageScroller gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:]: unrecognized selector sent to instance 0x21dd50

I would have thought that it should show

-[UIScrollView gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:]

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

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

Thanks for any insight.

解决方案

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 respondToSelector:返回true但实际调用super(selector)给出了发送给实例的“无法识别的选择器”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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