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

后端 未结 5 1700
不知归路
不知归路 2021-01-01 12:40

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 s

5条回答
  •  既然无缘
    2021-01-01 13:43

    As a summary for someone with the same case, there are two problems in the original question:

    1. Checking whether the superclass responds to that selector, which as suggested by @jjburka is best done by using instancesRespondToSelector:.
    2. Actually invoking the method on the superclass without the compiler complaining even though it's declared in a private header. This can be neatly achieved by redeclaring it in a category (see this question).

    Putting it together this gives:

    // … In subclass implementation file
    @interface UIScrollView ()  @end
    
    // … In gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
    if ([UIScrollView instancesRespondToSelector:@selector(gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:)]) {
        return [super gestureRecognizer:gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:otherGestureRecognizer];
    }
    

提交回复
热议问题