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];
代碼下載