I have an object which defines a set of protocols and delegate methods
Another object(s) respond to them in the usual pattern delegate pattern
@interface object
@property (nonatomic,weak) id <myProtocol> delegate;
@end
object.delegate = someObject;
@interface someObject <object delegate>
// some object delegate methods and their implementation
@end
In many cases - I would like to change the behavior of a particular delegate method (the implementation of the protocol)
In block based syntax I could easily assign a new "block" based on my considerations to the delegating object.
But in the standard delegate pattern this isn't possible. One way to solve the problem would be to create a large dispatch table ("if" or "switch" statement inside of the answering delegate) - but this would be awkward and make it very hard to understand the code.
it would be much easier to write something like
//standard case
theObject.delegate.blockForMethodOne = ^{the usual code to run} // perhaps how to update a UITableView
if (some condition happened) //something was selected something for instance
{
theObject.delegate.blockForMethodOne = ^ { some code to run in that case }
}
With out this type of syntax we would need to write something similar
-(void)methodOne
{
if (standard case)
{
//standard code
}
else if (self.someConditionHappended) // awkward variable in the object to track changes
{
// the code in this case
}
// and so on
}
I've seen answers but they aren't good enough.
Something that could dynamically generate a selector and block would be much better (a proxy delegate)
Any idea how to create this?
Edit:
Based on some blogs I created this sample class which will answer any delegate methods and forward them
@interface DelegateManager : NSObject
@property (nonatomic,weak) id proxiedObject;
@property (nonatomic) BOOL justResponded;
@property (nonatomic) BOOL logOnNoResponse;
-(id)init;
-(void)forwardInvocation:(NSInvocation*)invocation;
-(id)proxiedObject;
-(void)setProxiedObject:(id)proxied;
-(BOOL)justResponded;
-(void)setLogOnNoResponse:(BOOL)log;
-(BOOL)logOnNoResponse;
@end
@interface NSMethodSignature (objctypes)
+(NSMethodSignature*)signatureWithObjCTypes:(const char*)types;
@end
@implementation DelegateManager
-(id)init
{
self = [super init];
if (self)
{
self.proxiedObject = nil;
self.justResponded = NO;
self.logOnNoResponse = NO;
}
return self;
}
-(NSMethodSignature*)methodSignatureForSelector:(SEL)selector
{
NSMethodSignature *sig;
sig=[[self.proxiedObject class] instanceMethodSignatureForSelector:selector];
if(sig==nil)
{
// sig=[NSMethodSignature signatureWithObjCTypes:"@^v^c"];
sig = [[NSObject class] instanceMethodSignatureForSelector: @selector(init)];
}
self.justResponded=NO;
return sig;
}
-(void)forwardInvocation:(NSInvocation*)invocation
{
if(self.proxiedObject==nil)
{
if(self.logOnNoResponse)
NSLog(@"Warning: proxiedObject is nil! This is a debugging message!");
return;
}
if([self.proxiedObject respondsToSelector:[invocation selector]])
{
[invocation invokeWithTarget:self.proxiedObject];
self.justResponded=YES;
}
else if(self.logOnNoResponse)
{
NSLog(@"Object \"%@\" failed to respond to delegate message \"%@\"! This is a debugging message.",[[self proxiedObject] class],NSStringFromSelector([invocation selector]));
}
return;
}
@end
This code works as follows
myobject.delegate = self.delegateManager; // always sets itself to this internal "proxy" for the delegate
-(void)setDelegate(id<myProtocol>)delegate
{
[self.delegateManager setProxiedObject:delegate];
}
messages are always sent to the proxy which will try to pass them forward
myObject:
[self.delegateManager callADelegateMethod:self];
delegateManager is responsible for passing the message forward
it seems that this could be expanded upon by doing something like
if (something happened in my object)
{
// replace the implementation of the selector my delegate supplies
[self.myObject.delegateManager setBlock:someBlock forSelector:the selector of the delegate];
}
in delegateManager
-(void)setBlock:(someBlock)block forSelector:(selector)aSelector
{
[self.dictionary setObject:block forKey:aSelector];
}
-(void)forwardInvocation:(NSInvocation*)invocation
{
//check if there is an entrance of a block for the particular invocation
}
The question is how to create a key that will be good for this look up?