ios开发之block

ios开发之block

1.block的基本使用(语法)

//block的基本使用
- (void)block
{
    //block 理解为匿名函数
    //void func()
    //{
    //}
    
    //1.block变量的定义
    //技巧:  解决语法诡异带来难写的问题
    // void func();
    //定义block;  ^表示定义block,
    //技巧:函数名左右加括号,在函数名前面加^
    void(^block)();
    
    //定义block语句块,存储到了block变量中
    //实现
    block = ^void ()
    {
        NSLog(@"I am 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 (^myAdd)(int x,int y) = ^int (int x,int y)
    {
        return x+y;
    };
    int s = myAdd (10,20);
    NSLog(@"%d",s);
    
    //3.block捕获外部变量
    // block使用block外面的变量的注意事项
    int num = 10;
    __block int val = 100;
    void (^b1)() = ^void()
    {
        //1.block能使用和修改实例变量
        _page =1;
        
        //2.block中不能修改局部变量的值
        //num++;
        
        //3.block中能修改__block修饰的局部变量
        val++;
        
        //4.下面这样写有可能有警告,因为内存问题引起,注意
        //可以改为:
        //__weak typedef(self) weakSelf = self;//block外定义
        //weakSelf.url = @"txt";
        self.url = @"txt";
    };
    b1();
    //    NSLog(@"val = %d",val);

}

2.block在开发中的应用

2.1 可用于排序

    //1.NSMutableArray排序
    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 = @"wangcai";
    dahuang.age = 5;
    
    NSMutableArray *marr = [[NSMutableArray alloc] initWithArray:@[ahua,amiao,dahuang]];
//    marr sortUsingSelector:<#(SEL)#>
    
    //排序sortUsingComparator
    [marr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        Dog *aDog = obj1;
        Dog *bDog = obj2;
//        return aDog.age > bDog.age;
        return aDog.nickname > bDog.nickname;
    }];
    
    for (Dog *d in marr) {
        NSLog(@"name = %@  age = %d",d.nickname,d.age);
    }

2.2  可用于UIView执行简单动画

    //2.UIView动画
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 100, 100)];
    lable.text = @"我的Lable";
    lable.backgroundColor = [UIColor redColor];
    [self.view addSubview:lable];
    
    //向下移动200点
    //    [UIView animateWithDuration:<#(NSTimeInterval)#> animations:<#^(void)animations#>];
    [UIView animateWithDuration:2 animations:^{
        CGRect fram = lable.frame;
        fram.origin.x += 280;
        lable.frame = fram;
    } completion:^(BOOL finished) {
        NSLog(@"动画结束");
        [UIView animateWithDuration:1 animations:^{
            lable.transform = CGAffineTransformMakeRotation(M_PI);
        } completion:^(BOOL finished) {
//            NSLog(@"结束");
        }];
        
    }];
     

2.3  可用于界面的反向传值

假设有两个界面:A界面和B界面,程序从A界面跳转的B界面来设置A界面的背景颜色,设置完在返回A界面,从而A界面的背景颜色改为B界面的设置颜色

A界面有一个按钮,点击按钮进入B界面,按钮的点击响应为:

- (void)btnClick:(UIButton *)btn
{
    SecondViewController *svc = [[SecondViewController alloc] init];
    //设置block
    [svc setChangeBackgloundColor:^(NSString *color) {
        if ([color isEqualToString:@"blue"]) {
            self.view.backgroundColor = [UIColor blueColor];
            NSLog(@"修改颜色");
        }
    }];
    
    [self presentViewController:svc animated:YES completion:nil];
}

下面看看B界面的代码

@interface SecondViewController ()
{
    //定义block变量,为了保存传入的参数
    void (^_action)(NSString *color);
    
}
@end

B界面实现以下方法

- (void)setChangeBackgloundColor:(void (^)(NSString *))action
{
   _action = action;
}

B界面的也显示一个按钮,点击按钮返回A界面,按钮的点击响应为:

- (void)btnClick:(UIButton *)btn
{
    if (_action) {
        _action(@"blue");
        NSLog(@"存在_action");
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
原文地址:https://www.cnblogs.com/monian0313/p/4399198.html