block 理解及 简单回调
block 就是一个代码块,上代码:
TestBlock类中 就是定义block 给 block 传入参数 ,“方法”体里 留到回调的地方 实现
#import <Foundation/Foundation.h> typedef void (^BlockCallBack)(NSString *SayWhat); @interface TestBlock : NSObject { BlockCallBack bcb; } @property(nonatomic,assign)BlockCallBack bcb; -(void)initBcb:(id)entity:(BlockCallBack)bcb1; @end
#import "TestBlock.h" #import <objc/runtime.h> @implementation TestBlock @synthesize bcb; -(void)initBcb:(id)entity :(BlockCallBack)bcb1 { const char *name= object_getClassName(entity); self.bcb=bcb1; if (bcb) { bcb([[NSString alloc] initWithUTF8String:name]); }else { NSLog(@"参数 bcb1 未实现..."); } } -(void)dealloc { [super dealloc]; } @end
再joce 类中 (一人名 简单事例 不太规范)
回调 block 得到参数 实现 “方法”体
#import <Foundation/Foundation.h> #import "TestBlock.h" @interface Joce : NSObject { TestBlock *tb; } @property(nonatomic,retain)TestBlock *tb; -(void)sayHello; @end
#import "Joce.h" @implementation Joce @synthesize tb; -(void)sayHello { if (tb) { [tb initBcb:self :^(NSString *name){ NSLog(@"%@ Say Hello Word!!!!!!",name); }]; }else { NSLog(@"请实例化 joce"); } } -(void)dealloc { [tb release]; [super dealloc]; } @end
ok....
希望下次能再 项目中用到....