第一種直接寫:ide
@property (copy) void(^MyBlock)(void);優化
第二種:(typedef)atom
typedef void(^MyBlock)(void);spa
@property (nonatomic, copy) MyBlock block;.net
1) block內部可以一直引用全局變量指針
2) block內部可以一直引用被static修飾的變量code
3) block內部可以一直引用被__block修飾的變量orm
普通的局部變量,block內部只會引用它最近的值(block定義那一刻),不能跟蹤它的改變對象
void test(); int main(int argc, const char * argv[]) { test(); return 0; } int num = 10; void test5() { void (^block)() = ^{ // block內部可以一直引用全局變量 NSLog(@"----num=%d", num); //-->20 }; num = 20; block(); } void test4() { static int age = 10; void (^block)() = ^{ // block內部可以一直引用被static修飾的變量 NSLog(@"----age=%d", age); //-->20 }; age = 20; block(); } void test3() { __block int age = 10; void (^block)() = ^{ // block內部可以一直引用被__block修飾的變量 NSLog(@"----age=%d", age); //-->20 }; age = 20; block(); } void test2() { int age = 10; age = 20; void (^block)() = ^{ // 普通的局部變量,block內部只會引用它初始的值(block定義那一刻),不能跟蹤它的改變 NSLog(@"----age=%d", age); }; block(); } void test() { int age = 10; void (^block)() = ^{ // ----age=10 NSLog(@"----age=%d", age); //-->10 }; age = 20; block(); }
1 block的使用 AViewController.h @interface AViewController : UIViewController { UILabel *valueLabel;//⽤來接收SecondViewController傳回的值 } @end 實現AViewController.m - (void)viewDidLoad { [super viewDidLoad]; valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 300, 50)]; [self.view addSubview:valueLabel]; UIBarButtonItem *nextBtn = [[UIBarButtonItem alloc] initWithTitle:@"next" style:UIBarButtonItemStylePlain target:self action:@selector(nextClick)]; self.navigationItem.rightBarButtonItem = nextBtn; } - (void)nextClick { BViewController *svc = [[BViewController alloc] init]; svc.backValue = ^(NSString *strValue) {//設置SecondViewController⾥邊的block屬性, 這是本程序的關鍵 valueLabel.text = strValue; }; [self.navigationController pushViewController:svc animated:YES]; } BViewController.h @interface BViewController : UIViewController @property (nonatomic, copy) void (^backValue)(NSString *strValue); @property (nonatomic, retain) UITextField *text; @end 實現BViewController.m @implementation BViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; text = [[UITextField alloc] initWithFrame:CGRectMake(10, 50, 300, 40)]; text.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:text]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = CGRectMake(10, 110, 60, 30); [btn setTitle:@"返回" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } - (void)btnClick { self.backValue(self.text.text);//調⽤block⽅法 [self.navigationController popViewControllerAnimated:YES]; } @end 2 block的注意事項 1,block內的是強引用。當與控制器循環引用,需用弱指針 cell下載圖片 http://my.oschina.net/u/2346786/blog/483872
block傳值的思路blog
1,B要傳值,即創建block屬性 void (^backValue)(NSString *strValue);
而後實現它:self.backValue(要傳的值)
2,A要獲得B的傳值 即實現B的的屬性。
AViewController *aVC = [[AViewController alloc] init];
//經過block傳值,城市切換
aVC.backValue = ^(NSString *strValue) {
//設置SecondViewController⾥邊的block屬性,這是本程序的關鍵
self.cityName = strValue;
};
3,分析blcok屬性的寫法
返回值-名稱-參數
返回值-名稱-等號
@property (nonatomic, copy) void (^backValue) (NSString *strValue);
@property (nonatomic, copy) NSString *name; =
4,要配合界面跳轉使用
能夠在控制器使用delloc方法判斷有無內存泄露,注意block內對象的強引用,要改成弱引用