iOS開發之Block,iosblock
iOS開發之Block 1.什麼是block,block的作用
UI開發和網路常見功能實現回調, 按鈕的事件處理方法是回調方法, 網路下載後的回調處理
(1) 按鈕 target-action 一個方法傳入按鈕中
(2) 表格視圖 傳入一個指標self, 回調視圖控制器中的方法
(3) block 語句塊, 解決回調, 理解為"匿名函數", 定義在方法裡面
2.block的基本使用(文法)
涉及知識點:
定義block變數,定義block語句塊
block參數和傳回值
block捕獲外部變數(包括self)
//block 理解為匿名函數 //void func() //{ //} //1.block變數的定義 //技巧: 文法詭異 // void func(); //定義block變數,^表示定義block, //技巧: 函數名左右加括弧,在函數名前面加^ void (^block)(); //定義block語句塊,儲存到block變數中 block = ^void () { NSLog(@"block block block"); }; //執行block block(); //2.帶參數和傳回值的block //執行個體 實現計算兩數之和的block// int (^MyAdd)(int x,int y);// MyAdd = ^int(int x,int y)// {// return x+y;// }; int (^MyAdd)(int x,int y) = ^int(int x,int y) { return x+y; }; NSLog(@"%d",MyAdd(10,20)); //3.block捕獲外部變數 //block使用block外部變數的注意事項// int num = 10; __block int val = 100; __weak typeof(self) weakSelf = self; void (^b1)() = ^void() { //全域變數可以修改 _page = 10; //不可修改// num = 11; //__block變數可以修改 val = 101; //有可能有警告,因為記憶體問題引起,注意 //__weak typeof(self) weakSelf = self;//在block外面定義weakSelf //weakSelf.url = @"text";// self.url = @"text"; weakSelf.url = @"text"; NSLog(@"1%@",weakSelf.url); }; b1();
3.block在開發中應用(OC,UI,網路)(1)使用block實現排序
// // 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 sortUsingSelector:<#(SEL)#> [muArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { Dog *aDog = obj1; Dog *bDog = obj2;// return aDog.age>bDog.age; return [aDog.nickname compare:bDog.nickname]<0; }]; for(Dog *d in muArr) { NSLog(@"nickname:%@ age:%d",d.nickname,d.age); }
(2)使用block實現動畫
//2.UIView動畫 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; label.text = @"我是label"; label.backgroundColor = [UIColor redColor]; [self.view addSubview:label]; //// [UIView animateWithDuration:<#(NSTimeInterval)#> animations:<#^(void)animations#>]; [UIView animateWithDuration:2 animations:^{ CGRect frame = label.frame; frame.origin.y += 200; label.frame = frame; } completion:^(BOOL finished) { NSLog(@"動畫1結束"); [UIView animateWithDuration:1 animations:^{ label.transform = CGAffineTransformMakeRotation(M_PI); } completion:^(BOOL finished) { NSLog(@"動畫2結束"); }]; }];
(3)使用block實現介面傳值
若有兩個介面A介面、B介面, A介面建立B介面, B介面值傳遞到A介面
A介面的轉跳函數處設定block
- (void)click:(UIButton *)but{ SecondViewController *svc = [[SecondViewController alloc] init]; //設定block [svc setChanhgeBackgeoundColor:^(NSString *color) { if ([color isEqualToString:@"blue"]) { self.view.backgroundColor = [UIColor blueColor]; NSLog(@"改變主介面顏色"); } }]; [self presentViewController:svc animated:YES completion:nil];}
B介面儲存block
@interface SecondViewController : UIViewController// void action(NSString *color);// void (^action)(NSString *color);//為了給第二個介面傳入block-(void)setChanhgeBackgeoundColor:(void (^)(NSString *color))action;@end
B介面執行A介面傳遞來的block
//建立分類@interface SecondViewController (){ //定義一個block類型的變數,為了儲存傳入的參數 void (^_action)(NSString *color);}@end
//將傳過來的block存為全域變數- (void)setChanhgeBackgeoundColor:(void (^)(NSString *))action{ _action = action;}
//在點擊返回事件中執行block,因為block被全域變數儲存,所有blick可以在這個viewcontrol的任意地方執行- (void)click:(UIButton *)but{ //改變主介面的顏色 if (_action) { _action(@"blue"); NSLog(@"vc2點擊結束"); } [self dismissViewControllerAnimated:YES completion:nil];}
點擊下載完整代碼