1.通常我们这样用委托(Delegate):
// 声明一个委托协议
@protocol TestProtcol
- (NSString*) testDelegateMethod:(NSString*) aString;
@end
//实现一个委托协议
@interface TestDelegateImpl : NSObject
@end
@implementation TestDelegateImpl
- (NSString*) testDelegateMethod:(NSString *)aString
{
return aString;
}
@end

使用委托的类:
#import "TestProtcol.h"
//需要委托的测试类,例如tableview、scrollview等
@interface TestObject : NSObject
//维护一个委托对象
@property(nonatomic, weak) id delegate;
- (NSString*) callDelegate:(NSString*) aString;
@end
@implementation TestObject
@synthesize delegate = _delegate;
//调用委托方法获取返回结果
- (NSString*) callDelegate:(NSString*) aString
{
if ([_delegate respondsToSelector:@selector(testDelegateMethod:)]) {
return [_delegate testDelegateMethod:aString];
}
return nil;
}
@end

以上就是一般委托模式的实现,下面来做一下测试:
TestDelegateImpl* testDelegate = [[TestDelegateImpl alloc] init];
TestObject* testObject = [[TestObject alloc] init];
testObject.delegate = testDelegate;
//测试委托类是否能正常返回结果
NSLog(@"result:%@", [testObject callDelegate:@"testNormal"]);

2.但是,如果你使用了MUBlockDelegate…
不需要写委托协议,也不需要新建委托实现,你只需要新建一个BlockDelegate对象
//anInvocation用来处理返回结果,调用参数按照顺序放置在NSArray里面。
MUBlockDelegate* testDelegate = [MUBlockDelegate delegateForSelectorString:@"(@)testDelegateMethod(@)" delegateBlock:^(NSInvocation *anInvocation, NSArray *params) {
//这里便是委托方法的实现。
//简单地把第一个参数拿出来,返回。
NSString* testString = [params objectAtIndex:0];
[anInvocation setReturnValue:&testString];
}];

做一些测试:
TestObject* testObject = [[TestObject alloc] init];
testObject.delegate = (id)testDelegate;
NSString* testResult = [testObject callDelegate:@"testBlock"];
NSLog(@"testResult:%@", testResult);

程序正常输出:testBlock
另外,你还可以用过Class和Protocol来获得配置好的blockDelegate实例:
MUBlockDelegate* testDelegate = [MUBlockDelegate delegateForClass:[TestDelegateImpl class] selector:@selector(testDelegateMethod:) delegateBlock:^(NSInvocation *anInvocation, NSArray *params) {
NSString* testString = [params objectAtIndex:0];
[anInvocation setReturnValue:&testString];
}];
MUBlockDelegate* testDelegate = [MUBlockDelegate delegateForProtocol:@protocol(TestProtcol) selector:@selector(testDelegateMethod:) delegateBlock:^void(NSInvocation* anInvocation, NSArray *params) {
NSString* testString = [params objectAtIndex:0];
[anInvocation setReturnValue:&testString];
}];

3.源码下载:
下载地址: GitHub
[dl href=”http://vdisk.weibo.com/s/9WOLo”]点此下载完整源码[/dl]