国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

IOS開發之Block

2019-11-14 19:13:59
字體:
來源:轉載
供稿:網友

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];

 

代碼下載


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安多县| 屯门区| 康平县| 台东市| 罗源县| 嵩明县| 丁青县| 林芝县| 聂荣县| 成武县| 哈巴河县| 密云县| 博罗县| 新巴尔虎右旗| 郸城县| 乌拉特中旗| 南华县| 五家渠市| 和林格尔县| 西宁市| 琼结县| 库尔勒市| 沁水县| 遵义县| 桐梓县| 和政县| 德格县| 宁都县| 原平市| 南靖县| 堆龙德庆县| 阿拉尔市| 阿拉善盟| 怀宁县| 赤壁市| 广元市| 高雄县| 竹山县| 潮州市| 寻乌县| 梧州市|