UI開發和網絡常見功能實現回調,按鈕的事件處理方法是回調方法,網絡下砸后的回調處理
?。?)按鈕target-action 一個方法傳入按鈕中
?。?)表格視圖 傳入一個指針self,回調視圖控制器重的方法
?。?)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();
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];
新聞熱點
疑難解答