Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Sometimes it makes sense to attach several delegates to a scroll view. In that case you can build a simple delegation splitter:</p> <pre><code>// Public interface @interface CCDelegateSplitter : NSObject - (void) addDelegate: (id) delegate; - (void) addDelegates: (NSArray*) delegates; @end // Private interface @interface CCDelegateSplitter () @property(strong) NSMutableSet *delegates; @end @implementation CCDelegateSplitter - (id) init { self = [super init]; _delegates = [NSMutableSet set]; return self; } - (void) addDelegate: (id) delegate { [_delegates addObject:delegate]; } - (void) addDelegates: (NSArray*) delegates { [_delegates addObjectsFromArray:delegates]; } - (void) forwardInvocation: (NSInvocation*) invocation { for (id delegate in _delegates) { [invocation invokeWithTarget:delegate]; } } - (NSMethodSignature*) methodSignatureForSelector: (SEL) selector { NSMethodSignature *our = [super methodSignatureForSelector:selector]; NSMethodSignature *delegated = [(NSObject *)[_delegates anyObject] methodSignatureForSelector:selector]; return our ? our : delegated; } - (BOOL) respondsToSelector: (SEL) selector { return [[_delegates anyObject] respondsToSelector:selector]; } @end </code></pre> <p>Then simply set an instance of this splitter as a delegate of the scroll view and attach any number of delegates to the splitter. All of them will receive the delegation events. Some caveats apply, for example all the delegates are assumed to be of the same type, otherwise you’ll have trouble with the naive <code>respondsToSelector</code> implementation. This is not a big problem, it’s easy to change the implementation to only send delegation events to those who support them.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload