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

后端 未结 5 1713
不知归路
不知归路 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:25

    When you call

    [super respondsToSelector:@selector(gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:)]
    

    This is going to the superclass and running its implementation of respondsToSelector. This looks at the instance (in this case your custom scroll view) and determines if it responds to that selector or not.

    When you call

    [super gestureRecognizer:gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:otherGestureRecognizer];
    

    You try to send a message using the superclass implementation of that method, which in this case does not exist, causing the crash.

    Looks like jjburka got to it first - you need to call

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

    Also, it won't crash if you use -[super performSelector:] from unrecognized selector - perform selector gets the instances implementation of the selector. It will crash from infinite recursion.

提交回复
热议问题