Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple delegates for one object? (follow-up)
    primarykey
    data
    text
    <p>On the question <a href="https://stackoverflow.com/questions/8999818/multiple-delegates-per-one-object">Multiple delegates per one object?</a> one of the answers came up with interesting solution (at least for my naive eyes) : creating a "delegate splitter" that allows an object (in this case UIScrollView) to have multiple delegates (in this case UIScrollViewDelegate). </p> <p>The code is as follows:</p> <pre><code>@interface DelegateSplitter : NSObject -(void)addDelegate:(id)delegate; -(void)addDelegates:(NSArray*)array; @end @interface DelegateSplitter() @property NSMutableSet *delegates; @end @implementation DelegateSplitter -(id)init { self = [super init]; _delegates = [NSMutableSet set]; return self; } -(void)addDelegate:(id)delegate { [_delegates addObject:delegate]; } -(void)addDelegates:(NSArray *)array { [_delegates addObjectsFromArray:array]; } -(void)forwardInvocation:(NSInvocation *)anInvocation { for (id delegate in _delegates) { if([delegate respondsToSelector:anInvocation.selector]) { [anInvocation invokeWithTarget:delegate]; } } } - (NSMethodSignature*) methodSignatureForSelector: (SEL) selector { NSMethodSignature *our = [super methodSignatureForSelector:selector]; NSMethodSignature *delegated = [[_delegates anyObject] methodSignatureForSelector:selector]; return our ? our : delegated; } - (BOOL) respondsToSelector: (SEL) selector { return [[_delegates anyObject] respondsToSelector:selector]; } </code></pre> <p>The problem with this code is that it creates retain cycles, and unlike a normal delegate, you can't declare it as </p> <pre><code>@property (assign) DelegateSplitter *splitter; </code></pre> <p>It does seem however a "better" solution than a wrapper. So is there anyway to avoid the retain cycle ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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