IOS開發之Block,iosblock

來源:互聯網
上載者:User

IOS開發之Block,iosblock
IOS開發之Block1.什麼是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];}

  反向傳值:

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


 

代碼下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.