iOS开发之Block
1.什么是block,block的作用
UI开发和网络常见功能实现回调, 按钮的事件处理方法是回调方法, 网络下载后的回调处理
(1) 按钮 target-action 一个方法传入按钮中
(2) 表格视图 传入一个指针self, 回调视图控制器中的方法
(3) block 语句块, 解决回调, 理解为"匿名函数", 定义在方法里面
2.block的基本使用(语法)
涉及知识点:
定义block变量,定义block语句块
block参数和返回值
block捕获外部变量(包括self)
//block 理解为匿名函数 //void func() //{ //} //1.block变量的定义 //技巧: 语法诡异 // void func(); //定义block变量,^表示定义block, //技巧: 函数名左右加括号,在函数名前面加^ void (^block)(); //定义block语句块,存储到block变量中 block = ^void () { NSLog(@"block block block"); }; //执行block block(); //2.带参数和返回值的block //实例 实现计算两数之和的block // int (^MyAdd)(int x,int y); // MyAdd = ^int(int x,int y) // { // return x+y; // }; int (^MyAdd)(int x,int y) = ^int(int x,int y) { return x+y; }; NSLog(@"%d",MyAdd(10,20)); //3.block捕获外部变量 //block使用block外部变量的注意事项 // int num = 10; __block int val = 100; __weak typeof(self) weakSelf = self; void (^b1)() = ^void() { //全局变量可以修改 _page = 10; //不可修改 // num = 11; //__block变量可以修改 val = 101; //有可能有警告,因为内存问题引起,注意 //__weak typeof(self) weakSelf = self;//在block外面定义weakSelf //weakSelf.url = @"text"; // self.url = @"text"; weakSelf.url = @"text"; NSLog(@"1%@",weakSelf.url); }; b1();
3.block在开发中应用(OC,UI,网络)
(1)使用block实现排序
// // Dog *ahua = [[Dog alloc] init]; ahua.nickname = @"ahua"; ahua.age = 4; Dog *amiao = [[Dog alloc] init]; amiao.nickname = @"amiao"; amiao.age = 3; Dog *dahuang = [[Dog alloc] init]; dahuang.nickname = @"dahuang"; dahuang.age = 5; NSMutableArray *muArr = [[NSMutableArray alloc] initWithArray:@[ahua,amiao,dahuang]]; // muArr sortUsingSelector:<#(SEL)#> [muArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { Dog *aDog = obj1; Dog *bDog = obj2; // return aDog.age>bDog.age; return [aDog.nickname compare:bDog.nickname]<0; }]; for(Dog *d in muArr) { NSLog(@"nickname:%@ age:%d",d.nickname,d.age); }
(2)使用block实现动画
//2.UIView动画 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; label.text = @"我是label"; label.backgroundColor = [UIColor redColor]; [self.view addSubview:label]; // // [UIView animateWithDuration:<#(NSTimeInterval)#> animations:<#^(void)animations#>]; [UIView animateWithDuration:2 animations:^{ CGRect frame = label.frame; frame.origin.y += 200; label.frame = frame; } completion:^(BOOL finished) { NSLog(@"动画1结束"); [UIView animateWithDuration:1 animations:^{ label.transform = CGAffineTransformMakeRotation(M_PI); } completion:^(BOOL finished) { NSLog(@"动画2结束"); }]; }];
(3)使用block实现界面传值
若有两个界面A界面、B界面, A界面创建B界面, B界面值传递到A界面
A界面的转跳函数处设置block
- (void)click:(UIButton *)but { SecondViewController *svc = [[SecondViewController alloc] init]; //设置block [svc setChanhgeBackgeoundColor:^(NSString *color) { if ([color isEqualToString:@"blue"]) { self.view.backgroundColor = [UIColor blueColor]; NSLog(@"改变主界面颜色"); } }]; [self presentViewController:svc animated:YES completion:nil]; }
B界面保存block
@interface SecondViewController : UIViewController // void action(NSString *color); // void (^action)(NSString *color); //为了给第二个界面传入block -(void)setChanhgeBackgeoundColor:(void (^)(NSString *color))action; @end
B界面执行A界面传递来的block
//创建分类 @interface SecondViewController () { //定义一个block类型的变量,为了保存传入的参数 void (^_action)(NSString *color); } @end
//将传过来的block存为全局变量 - (void)setChanhgeBackgeoundColor:(void (^)(NSString *))action { _action = action; }
//在点击返回事件中执行block,因为block被全局变量保存,所有blick可以在这个viewcontrol的任意地方执行 - (void)click:(UIButton *)but { //改变主界面的颜色 if (_action) { _action(@"blue"); NSLog(@"vc2点击结束"); } [self dismissViewControllerAnimated:YES completion:nil]; }