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

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

    [super respondsToSelector:@selector(frobnosticate:)] doesn't do what you think.

    It goes to the superclass, gets the implementation of respondsToSelector: there, and then runs it on the current object. In other words, super represents the same object as self, it just starts the method lookup one step higher in the inheritance tree.

    So you're running respondsToSelector: on this subclass, which replies "YES", but then later trying to get gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: from the superclass, which doesn't have it.

    To check instances of the immediate superclass, you would use instancesRespondToSelector:, as jjburka recommend, but I would suggest [self superclass] as the subject of that, like so:

    [[self superclass] instancesRespondToSelector:@selector(gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:)];
    

    which avoids hardcoding class names.

提交回复
热议问题