//
// BlocksDemo.h
// OC基础学习
//
// Created by 麦子 on 15/6/8.
// Copyright (c) 2015年 麦子. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface BlocksDemo : NSObject
- (id)init;
- (void)setBlocks;
- (void)blockCallBackDemo;
@end
//
// BlocksDemo.m
// OC基础学习
//
// Created by 麦子 on 15/6/8.
// Copyright (c) 2015年 麦子. All rights reserved.
//
#import "BlocksDemo.h"
#import "Water.h"
#import "ChilderPerson.h"
typedef int (^SumBlock) (int a,int b);
@implementation BlocksDemo
- (id)init{
self = [super init];
if (self != nil) {
[self setBlocks];
}
return self;
}
/*****
Block 用法
这种形式和Java处理中类似于,在一个方法中,传入一个接口类。 在我们需要他的时候,需要实现这个接口中的方法。
这种形式就是相当于java中的回调的写法。方法传入接口或者是set设置接口类。
在调用的地方,才去实现它。
****/
- (void)setBlocks{
// 没有参数的
void (^myblocks) (void) = nil;
myblocks = ^(void){
NSLog(@"没有参数的block调用");
};
myblocks(); // 调用
// 有参数的形式
int (^myblocks2)(int a,int b) = ^(int a, int b){
return a + b;
};
int val = myblocks2(10,10);
NSLog(@"有参数的值val--%d",val);
// 修改外部的值
__block int count = 100;
int (^myblock3)(int a,int b) = ^(int a,int b){
count = a + b;
return count;
};
myblock3(300,100);
NSLog(@"修改外部变量值---%d",count);
SumBlock sumblcok = ^(int a,int b){
NSLog(@"typedef定义---count---%d",a+b);
return a+b;
};
sumblcok(10,90);
}
/***
回调处理: 实现, 水的深度在不停的升高, 小孩的不同的反应。 对于这一块的业务处理在小孩的这个类。
*/
- (void)blockCallBackDemo{
ChilderPerson *person = [[ChilderPerson alloc] init];
Water *water = [[Water alloc] init];
WaterCallBack callback = ^(int waterDeepVal){
NSLog(@"回调成功----%d-----",waterDeepVal);
};
[water setCallback:callback];
[person setWater:water];
[[NSRunLoop currentRunLoop] run];
}
@end
//
// Water.h
// OC基础学习
//
// Created by 麦子 on 15/6/8.
// Copyright (c) 2015年 麦子. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef void (^WaterCallBack) (int waterDeepVal);
@interface Water : NSObject
@property(nonatomic) int waterDeepVal;
@property(nonatomic) WaterCallBack callback;
- (id)init;
@end
//
// Water.m
// OC基础学习
//
// Created by 麦子 on 15/6/8.
// Copyright (c) 2015年 麦子. All rights reserved.
//
#import "Water.h"
@implementation Water
@synthesize waterDeepVal;
@synthesize callback;
- (id)init{
self = [super init];
if (self != nil) {
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeAction:) userInfo:nil repeats:YES];
}
return self;
}
- (void)timeAction:(NSTimer *) timer{
self.waterDeepVal++;
NSLog(@"水深----%d",self.waterDeepVal);
callback(self.waterDeepVal);// 在这里处理回调
}
@end
//
// ChilderPerson.h
// OC基础学习
//
// Created by 麦子 on 15/6/8.
// Copyright (c) 2015年 麦子. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Water.h"
@interface ChilderPerson : NSObject
@property(nonatomic,retain) Water *water;
@end
//
// ChilderPerson.m
// OC基础学习
//
// Created by 麦子 on 15/6/8.
// Copyright (c) 2015年 麦子. All rights reserved.
//
#import "ChilderPerson.h"
@implementation ChilderPerson
@synthesize water;
@end
BlocksDemo *blockDemo = [[BlocksDemo alloc] init];
[blockDemo blockCallBackDemo];
运行结果如下:
2015-06-08 15:40:52.344 OC基础学习[1904:119454] 没有参数的block调用
2015-06-08 15:40:52.345 OC基础学习[1904:119454] 有参数的值val--20
2015-06-08 15:40:52.345 OC基础学习[1904:119454] 修改外部变量值---400
2015-06-08 15:40:52.345 OC基础学习[1904:119454] typedef定义---count---100
2015-06-08 15:40:53.350 OC基础学习[1904:119454] 水深----1
2015-06-08 15:40:53.350 OC基础学习[1904:119454] 回调成功----1-----
2015-06-08 15:40:54.347 OC基础学习[1904:119454] 水深----2
2015-06-08 15:40:54.347 OC基础学习[1904:119454] 回调成功----2-----
2015-06-08 15:40:55.350 OC基础学习[1904:119454] 水深----3
2015-06-08 15:40:55.351 OC基础学习[1904:119454] 回调成功----3-----
Program ended with exit code: 9