简单的block入门
2016-05-27 本文已影响82人
静花寒
block的目的就是传值,今天写个最简单的block,希望看官看后有所启发.
界面2:
界面2.h
#import <Foundation/Foundation.h>
typedef void (^succeccBlock)(NSDictionary *responseDic);
@interface block : NSObject
- (void)success:(succeccBlock)successBlock;
@end
界面2.m
#import "block.h"
@implementation block
- (void)success:(succeccBlock)successBlock{
NSDictionary *resultDic = @{@"key":@"123"};
successBlock(resultDic);
}
@end
接下来就是接收数据了
界面1.m
- (void)viewDidLoad {
[super viewDidLoad];
block *block1 = [[block alloc] init];
[block1 success:^(NSDictionary *responseDic) {
NSString *value = responseDic[@"key"];
NSLog(@"%@",value);
}];
// Do any additional setup after loading the view, typically from a nib.
}
打印结果
49148F81-8C62-4D34-B5E5-EA918E7D4E14.png