iOS开发之Block
1.什么是block,block的作用
block是OC中的一种数据类型,同其他语言的闭包,或者lambda类似。
block在UI开发和网络常见功能实现回调,按钮的时间处理方法是回调方法,网络下载完成回调处理
(1)按钮target-action 一个方法传入按钮中
(2)表格视图 传入一个指针self,回调视图控制器中的方法
(3)block 语句块,解决回调,理解为“匿名函数”,定义在方法里面
2.block的基本使用(语法)
定义block变量
格式说明:
(返回类型)(^块名称)(参数类型) = ^(参数列表) {代码实现};
如果没有参数,等号后面参数列表的()可以省略
定义block语句块
block参数和返回值
block捕获外部变量(包括self)
block注意事项
-(void)basicUse{ /* 1.block变量的定义 技巧:解决语法诡异带来难写的问题 void func(); 定义block,^表示定义block 技巧: 函数名左右加括号,在函数名前面再加^ */ void (^block)(); //定义block语句块,存储到了block变量中 block = ^void () { NSLog(@"i am block"); }; //无参数,可以简写 // block = ^{ // NSLog(@"i am block"); // }; block(); //2.带有参数和返回值的block //实例 实现计算两数之和的block //int myAdd(int x,int y); int (^myAdd) (int x,int y) = ^(int x,int y){ return x+y; }; NSLog(@"%d", myAdd(10,29)); //3.block捕获外部变量 // block使用block外面的变量的注意事项 // int num = 10; //不能再block中修改 __block int val = 10; void (^b1)()= ^void(){ //能使用和修改实例变量 _page = 1; //block中不能修改局部变量的值 //num++; //block中修改__block修饰的局部变量 val++; //可能有警告,因为内存问题引起,self引用block,block保存self,造成循环引用,注意: //self.url = @"txt"; //修改方式:使用弱指针引用 __weak typeof (self) weakSelf = self; weakSelf.url = @"txt"; }; }
3.block在开发中的应用(OC,UI,网络)
使用block实现排序
-(void)blockDelelopApply { //oc中应用 //1.NSMutableArray排序 NLDog *ahua = [[NLDog alloc]init]; ahua.nickName = @"ahua"; ahua.age = 4; NLDog *amiao = [[NLDog alloc]init]; amiao.nickName = @"amiao"; amiao.age = 3; NLDog *dahuang = [[NLDog alloc]init]; dahuang.nickName = @"dahuang"; dahuang.age = 5; NSMutableArray *marr = [@[ahua,amiao,dahuang]mutableCopy]; // [marr sortedArrayUsingSelector:@selector(selector)]; [marr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { NLDog *aDog = obj1; NLDog *bDog = obj2; return aDog.age < bDog.age; }]; for (NLDog *dog in marr) { NSLog(@"name = %@ age = %d",dog.nickName,dog.age); } }
实现数组的枚举
NSArray *array = @[@"张三", @"李四", @"王五", @"赵六"]; [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSLog(@"第 %d 项内容是 %@", (int)idx, obj); if ([@"王五" isEqualToString:obj]) { *stop = YES; } }];
使用block实现动画
-(void)animationUIView{ UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 100, 100)]; label.text = @"我是label"; label.backgroundColor =[UIColor redColor]; [self.view addSubview:label]; // [UIView animateWithDuration:1.0 animations:^{ // // }]; [UIView animateWithDuration:1.0 animations:^{ CGRect rect = label.frame; rect.origin.y += 200; label.frame =rect; } completion:^(BOOL finished) { [UIView animateWithDuration:1.0 animations:^{ label.transform = CGAffineTransformMakeRotation(M_PI); CGRect rect = label.frame; rect.origin.y -= 200; label.frame =rect; } completion:^(BOOL finished) { NSLog(@"animation finished"); }]; }]; }
使用block实现界面传值
若有两个界面A界面, B界面, A界面创建B界面, B界面值传递到A界面
A界面设置block
//block实现界面反向传值 -(void)passValue{ UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)]; button.frame = CGRectMake(100, 100, 100, 50); [button setTitle:@"去" forState:(UIControlStateNormal)]; [self.view addSubview:button]; [button addTarget:self action:@selector(btnClick:) forControlEvents:(UIControlEventTouchUpInside)]; } -(void)btnClick:(UIButton *)btn{ NLSecondController *controller = [[NLSecondController alloc]init]; controller.action = ^(NSString *color) { if ([color isEqualToString:@"blue"]) { self.view.backgroundColor = [UIColor redColor]; } }; [self presentViewController:controller animated:YES completion:^{ controller.view.backgroundColor = [UIColor yellowColor]; }]; }
B界面保存block
@interface NLSecondController : UIViewController //void action(NSString *color); //-(void)setChangeBackgroundColor:(void (^)(NSString *color))action; @property (nonatomic,strong) void(^action)(NSString *color) ; @end @interface NLSecondController () @end @implementation NLSecondController - (void)viewDidLoad { [super viewDidLoad]; UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)]; button.frame = CGRectMake(100, 100, 100, 50); [button setTitle:@"回" forState:(UIControlStateNormal)]; [self.view addSubview:button]; [button addTarget:self action:@selector(btnClick:) forControlEvents:(UIControlEventTouchUpInside)]; } -(void)btnClick:(UIButton *)btn{ if (_action) { _action(@"blue"); } [self dismissViewControllerAnimated:YES completion:nil]; } @end
代码下载:点我下载