Search code examples
objective-cdelegatesuigesturerecognizersuperrespondstoselector

super respondsToSelector: returns true but actually calling super (selector) gives "unrecognized selector sent to instance"


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.


Solution

  • 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:)]