标签:
回调(按钮的处理事件,网络下载后的回调处理)
(1)按钮target-action 方法传入按钮中
(2)表格视图 传入self指针
(3)block 语句块,解决回调
//1.block变量的定义 //技巧:语法诡异 //定义block -->类似 void (*func)(); 函数指针 void (^block)(); //定义语句块 () block = ^void (){ //定义block语句块-->存到block变量中 NSLog(@"I am a block"); }; block(); //执行 //2. eg:计算两数之和 int myAdd(int a,int b); int (^myAdd) (int a,int b) = ^int(int a,int b){ return a+b; }; int sum = myAdd(10,30); NSLog(@"sum = %d",sum); //3.block捕获外部变量 --> 不能修改局部变量的值 ,可以使用和修改实例变量 // __block int a;(外部变量) // int num = 10; __block int value = 100; void (^b1)() = ^void(){ _page = 1; value++; // num++; //error //可能有警告,因内存问题,注: // __weak typeof(self) weakSelf = self; //block外定义 // weakSelf.url = @"text"; self.url = @"text"; //相互引用造成的内存问题 }; b1();
#import "Dog.h"
//oc应用 //1.NSMutableArray排序 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 sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { Dog *adog = obj1; Dog *bdog = obj2; //return adog.age > bdog.age; return [adog.nickName compare:bdog.nickName]; //按nickName排 }]; for(Dog *dog in muarr){ NSLog(@"name = %@,age = %d",dog.nickName,dog.age); }
//2.UIView动画 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; label.text = @"I am a label"; label.backgroundColor = [UIColor redColor]; [self.view addSubview:label]; [UIView animateWithDuration:2 animations:^{ CGRect rect = label.frame; rect.origin.y += 200; label.frame = rect; } completion:^(BOOL finished) { //可嵌套 NSLog(@"结束动画"); [UIView animateWithDuration:2 animations:^{ label.transform = CGAffineTransformMakeRotation(M_PI); //旋转180 } completion:^(BOOL finished) { }]; }];
#import <UIKit/UIKit.h> @interface SecondViewController : UIViewController //void action(NSString *color); //void (^action)(NSString *color); -(void)setChangeBackgroundColor:(void(^)(NSString *color))action; @end
#pragma mark - -(void)setChangeBackgroundColor:(void (^)(NSString *))action{ _action = action; } #pragma mark - - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //反向传值 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = CGRectMake(100, 200, 100, 30); //根据实际调 [btn setTitle:@"返回" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } -(void)btnClick:(UIButton *)btn{ if(_action){ _action(@"blue"); //逆向传值 } [self dismissViewControllerAnimated:YES completion:nil]; }
在第一个ViewConreoller接收
-(void)btnClick:(UIButton *)btn{ SecondViewController *svc = [[SecondViewController alloc] init]; svc.view.backgroundColor = [UIColor yellowColor]; [svc setChangeBackgroundColor:^(NSString *color) { if([color isEqualToString:@"blue"]){ self.view.backgroundColor = [UIColor blueColor]; } }]; [self presentViewController:svc animated:YES completion:nil]; }
标签:
原文地址:http://www.cnblogs.com/wlrBlogs/p/4397579.html