IOS开发之Block
IOS开发之Block
1.什么是block,block的作用
UI开发和网络常见功能实现回调,按钮的事件处理方法是回调方法,网络下砸后的回调处理
(1)按钮target-action 一个方法传入按钮中
(2)表格视图 传入一个指针self,回调视图控制器重的方法
(3)block 语句块,解决回调,理解为“匿名函数”,这个函数定义在方法里面
2.block的基本使用(语法)
定义block变量
定义block语句块
//block 理解匿名函数 //void func() //{ //} //1.block变量的定义 //技巧: 语法诡异带来男鞋的问题 //void func(); //定义block变量,^表示定义block //技巧: 函数名左右家括号,在函数名前面加^ void (^block)(); //定义block语句块,存储到block变量中 block = ^void () { NSLog(@"I am block"); }; //执行 block();
block参数和返回值
//2.带有参数和返回值block //实例 实现计算两数之和block // int myAdd(int x ,int y); int (^myAdd)(int x ,int y) = ^int (int x ,int y) { return x+y; }; int s = myAdd(10,20); NSLog(@"s = %d",s);
block捕获外部变量
block的注意事项
@interface ViewController (){ int _page; } @property (copy,nonatomic) NSString *url; @end //3.block是捕获外部变量 // block使用block外面的变量的注意事项 int num = 10; __block int val = 100; void (^bbbb)() = ^void() { //能使用和修改实例变量 _page = 1; // block中不能修改局部变量的值,但可以使用 //num++; //block中能修改__block修饰的局部变量 val++; //有可能有警告,因为内存问题引起,注意 // __weak typeof(self) weakSelf = self; // weakSelf.url = @"text"; self.url = @"text"; }; bbbb();
3.block在开发中应用(OC,UI,网络)
1.NSMutableArray排序
2.UIView动画
3.block实现界面反向传值
-(void)blockDelelopApply{ //oc中的应用 //1.NSMutableArray排序 Dog *ahua = [[Dog alloc]init]; ahua.nikeName = @"ahua"; ahua.age = 4; Dog *amiao = [[Dog alloc]init]; amiao.nikeName = @"amiao"; amiao.age = 3; Dog *dahuang = [[Dog alloc]init]; dahuang.nikeName = @"dahuang"; dahuang.age = 5; NSMutableArray *marr = [[NSMutableArray alloc]initWithArray:@[ahua,amiao,dahuang]]; //marr sortUsingSelector:<#(SEL)#> [marr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { Dog *aDog = obj1; Dog *bDog = obj2; // return aDog.age > bDog.age; return [aDog.nikeName compare:bDog.nikeName] > 0; }]; for (Dog *d in marr) { NSLog(@"name = %@,age = %d",d.nikeName,d.age); } //2.UIView动画 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 200, 100, 100)]; label.text = @"kkkk"; label.backgroundColor = [UIColor redColor]; [self.view addSubview:label]; //向下移动200 // [UIView animateWithDuration:<#(NSTimeInterval)#> animations:<#^(void)animations#>] [UIView animateWithDuration:2 animations:^{ CGRect frame = label.frame; frame.origin.x +=200; label.frame = frame; } completion:^(BOOL finished) { NSLog(@"finish"); [UIView animateWithDuration:1 animations:^{ label.transform = CGAffineTransformMakeRotation(M_PI); } completion:^(BOOL finished) { [UIView animateWithDuration:2 animations:^{ label.frame = CGRectMake(10, 200, 100, 100); label.transform = CGAffineTransformMakeRotation(M_PI); }]; }]; }]; //3.block实现界面反向传值 UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(0, 20, 320, 10); [button setTitle:@"change" forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonact:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } -(void)buttonact:(UIButton *)but{ SecondViewController *svc = [[SecondViewController alloc]init]; //设置block [svc setChangeBackGroundColor:^(NSString *color) { if ([color isEqualToString:@"blue"]) { self.view.backgroundColor = [UIColor blueColor]; } }]; [self presentViewController:svc animated:YES completion:nil]; }
反向传值:
使用block实现界面传值
若有两个界面A界面, B界面, A界面创建B界面, B界面值传递到A界面
A界面设置block,B界面保存block
a.在第二个界面定义block
//为了给第二个界面传入block -(void)setChangeBackGroundColor:( void (^)(NSString *color) )action; //@property (nonatomic,copy) setChangeBackGroundColor ( void (^__)(NSString *__color) );
b.第二个界面实现block
@interface SecondViewController (){ //定义block变量,为了保存传入的参数 void (^_action)(NSString *color); } @end @implementation SecondViewController -(void)setChangeBackGroundColor:(void (^)(NSString *))action{ _action = action; }
c.第二个界面给block赋值
//改变住界面的颜色 if (_action) { _action(@"blue"); }
d.第一个界面设置block
SecondViewController *svc = [[SecondViewController alloc]init];
//设置block [svc setChangeBackGroundColor:^(NSString *color) { if ([color isEqualToString:@"blue"]) { self.view.backgroundColor = [UIColor blueColor]; } }];
[self presentViewController:svc animated:YES completion:nil];