zoukankan      html  css  js  c++  java
  • IOS之block

    IOS之block

    1.block

      回调(按钮的处理事件,网络下载后的回调处理)

      (1)按钮target-action  方法传入按钮中

      (2)表格视图   传入self指针

      (3)block  语句块,解决回调

     2.block语句

        //1.block变量的定义
        //技巧:语法诡异    
        //定义block  -->类似 void (*func)(); 函数指针
        void (^block)();    
        //定义语句块  ()
        block = ^void (){   //定义block语句块-->存到block变量中
         
            NSLog(@"I am a block");
        };
        block();   //执行
        
        //2. eg:计算两数之和   int myAdd(int a,int b);
        int (^myAdd) (int a,int b) = ^int(int a,int b){
        
            return a+b;
        };
        int sum = myAdd(10,30);
        NSLog(@"sum = %d",sum);
        
        //3.block捕获外部变量  --> 不能修改局部变量的值 ,可以使用和修改实例变量
        // __block int a;(外部变量)
    //    int num = 10;
        __block int value = 100;
        void (^b1)() = ^void(){
        
            _page = 1;
            value++;
    //        num++;    //error        
            //可能有警告,因内存问题,注:
            //  __weak typeof(self) weakSelf = self;   //block外定义
            //  weakSelf.url = @"text";
            self.url = @"text";   //相互引用造成的内存问题
        };
        
        b1();

     3.block应用

      (1)NSMutableArray排序

    #import "Dog.h"
    //oc应用
        //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 = @"dahuang";
        dahuang.age = 5;
    
        NSMutableArray *muarr = [[NSMutableArray alloc] initWithArray:@[ahua,amiao,dahuang]];
        [muarr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
         
            Dog *adog = obj1;
            Dog *bdog = obj2;
            //return adog.age > bdog.age;
            return [adog.nickName compare:bdog.nickName];   //按nickName排
        }];
        
        for(Dog *dog in muarr){
        
            NSLog(@"name = %@,age = %d",dog.nickName,dog.age);
        }

      (2)UIView动画

     //2.UIView动画
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
        label.text = @"I am a label";
        label.backgroundColor = [UIColor redColor];
        [self.view addSubview:label];
        
        [UIView animateWithDuration:2 animations:^{
            CGRect rect = label.frame;
            rect.origin.y += 200;
            label.frame = rect;
        } completion:^(BOOL finished) {   //可嵌套
            NSLog(@"结束动画");
           
            [UIView animateWithDuration:2 animations:^{
                label.transform = CGAffineTransformMakeRotation(M_PI);   //旋转180
            } completion:^(BOOL finished) {
                
            }];
        }];

      (3)反向传值

    #import <UIKit/UIKit.h>
    @interface SecondViewController : UIViewController
    //void action(NSString *color);
    //void (^action)(NSString *color);
    -(void)setChangeBackgroundColor:(void(^)(NSString *color))action;
    @end
    #pragma mark -
    -(void)setChangeBackgroundColor:(void (^)(NSString *))action{
        
        _action = action;
    }
    
    #pragma mark -
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        //反向传值
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame = CGRectMake(100, 200, 100, 30);    //根据实际调
        [btn setTitle:@"返回" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
        
    }
    -(void)btnClick:(UIButton *)btn{
        
        if(_action){
        
            _action(@"blue");  //逆向传值
        }
        [self dismissViewControllerAnimated:YES completion:nil];
    }

       在第一个ViewConreoller接收

    -(void)btnClick:(UIButton *)btn{
    
        SecondViewController *svc = [[SecondViewController alloc] init];
            svc.view.backgroundColor = [UIColor yellowColor];
        [svc setChangeBackgroundColor:^(NSString *color) {
            if([color isEqualToString:@"blue"]){
            
                self.view.backgroundColor = [UIColor blueColor];
            }
        }];
        
        [self presentViewController:svc animated:YES completion:nil];
    }
  • 相关阅读:
    Python学习札记(十五) 高级特性1 切片
    LeetCode Longest Substring Without Repeating Characters
    Python学习札记(十四) Function4 递归函数 & Hanoi Tower
    single number和变体
    tusen 刷题
    实验室网站
    leetcode 76. Minimum Window Substring
    leetcode 4. Median of Two Sorted Arrays
    leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions 、434. Number of Islands II(lintcode) 并查集 、178. Graph Valid Tree(lintcode)
    刷题注意事项
  • 原文地址:https://www.cnblogs.com/wlrBlogs/p/4397579.html
Copyright © 2011-2022 走看看