我有一个对象,它定义了一组协议和委托方法
另一个对象以通常的模式委托模式响应它们
@interface object
@property (nonatomic,weak) id <myProtocol> delegate;
@end
object.delegate = someObject;
@interface someObject <object delegate>
// some object delegate methods and their implementation
@end
在许多情况下 - 我想更改特定委托方法的行为(协议的实现)
在基于块的语法中,我可以根据我对委托对象的考虑轻松地分配一个新的“块”。
但在标准委托模式中,这是不可能的。解决问题的一种方法是创建一个大型调度表(在应答委托中创建“if”或“switch”语句)——但这会很尴尬,并且很难理解代码。
写类似的东西会容易得多
//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 }
}
如果没有这种类型的语法,我们需要编写类似的东西
-(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
}
我已经看到了答案,但它们还不够好。
可以动态生成选择器和块的东西会更好(代理委托)
知道如何创建这个吗?
编辑:
基于一些博客,我创建了这个示例类,它将回答任何委托方法并转发它们
@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
此代码的工作原理如下
myobject.delegate = self.delegateManager; // always sets itself to this internal "proxy" for the delegate
-(void)setDelegate(id<myProtocol>)delegate
{
[self.delegateManager setProxiedObject:delegate];
}
消息总是发送到代理,代理会尝试将它们转发
我的对象:
[self.delegateManager callADelegateMethod:self];
delegateManager 负责转发消息
似乎这可以通过做类似的事情来扩展
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];
}
在委托管理器中
-(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
}
问题是如何创建一个有利于此查找的密钥?