UI開發(fā)和網(wǎng)絡(luò)常見功能實現(xiàn)回調(diào),按鈕的事件處理方法是回調(diào)方法,網(wǎng)絡(luò)下砸后的回調(diào)處理
(1)按鈕target-action 一個方法傳入按鈕中
(2)表格視圖 傳入一個指針self,回調(diào)視圖控制器重的方法
(3)block 語句塊,解決回調(diào),理解為“匿名函數(shù)”,這個函數(shù)定義在方法里面
定義block變量
定義block語句塊
//block 理解匿名函數(shù) //void func() //{ //} //1.block變量的定義 //技巧: 語法詭異帶來男鞋的問題 //void func(); //定義block變量,^表示定義block //技巧: 函數(shù)名左右家括號,在函數(shù)名前面加^ void (^block)(); //定義block語句塊,存儲到block變量中 block = ^void () { NSLog(@"I am block"); }; //執(zhí)行 block();
block參數(shù)和返回值
//2.帶有參數(shù)和返回值block //實例 實現(xiàn)計算兩數(shù)之和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++; //有可能有警告,因為內(nèi)存問題引起,注意// __weak typeof(self) weakSelf = self;// weakSelf.url = @"text"; self.url = @"text"; }; bbbb();
1.NSMutableArray排序
2.UIView動畫
3.block實現(xiàn)界面反向傳值
-(void)blockDelelopApply{ //oc中的應(yīng)用 //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實現(xiàn)界面反向傳值 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]; //設(shè)置block [svc setChangeBackGroundColor:^(NSString *color) { if ([color isEqualToString:@"blue"]) { self.view.backgroundColor = [UIColor blueColor]; } }]; [self presentViewController:svc animated:YES completion:nil];}
反向傳值:
使用block實現(xiàn)界面?zhèn)髦?/span>
若有兩個界面A界面, B界面, A界面創(chuàng)建B界面, B界面值傳遞到A界面
A界面設(shè)置block,B界面保存block
a.在第二個界面定義block
//為了給第二個界面?zhèn)魅隻lock-(void)setChangeBackGroundColor:( void (^)(NSString *color) )action;//@property (nonatomic,copy) setChangeBackGroundColor ( void (^__)(NSString *__color) );
b.第二個界面實現(xiàn)block
@interface SecondViewController (){ //定義block變量,為了保存?zhèn)魅氲膮?shù) void (^_action)(NSString *color);}@end@implementation SecondViewController-(void)setChangeBackGroundColor:(void (^)(NSString *))action{ _action = action;}
c.第二個界面給block賦值
//改變住界面的顏色 if (_action) { _action(@"blue"); }
d.第一個界面設(shè)置block
SecondViewController *svc = [[SecondViewController alloc]init];
//設(shè)置block [svc setChangeBackGroundColor:^(NSString *color) { if ([color isEqualToString:@"blue"]) { self.view.backgroundColor = [UIColor blueColor]; } }];
[self presentViewController:svc animated:YES completion:nil];
新聞熱點
疑難解答