iOS笔记 - 利用block实现页面传值(参数传值 | 属性传值)


页面传值

1 - 代码示例:当前页面里创建一个 label,显示从下个页面中所传回的值

// - ViewController.m

 1 #import "ViewController.h"
 2 #import "SecondViewController.h"
 3 @interface ViewController()
 4 @property(nonatomic,strong)UILabel *label;
 5 @end
 6 
 7 @implementation ViewController
 8 
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11 
12     UIButton *nextBT = [UIButton buttonWithType:UIButtonTypeCustom];
13     CGRect frame = nextBT.frame;
14     frame.size.height = 40;
15     frame.size.width = 100;
16     nextBT.frame = frame;
17     nextBT.center = self.view.center;
18     [nextBT setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
19     [nextBT setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
20     [nextBT setTitle:@"下一页" forState:UIControlStateNormal];
21     nextBT.backgroundColor = [UIColor cyanColor];
22     [nextBT addTarget:self action:@selector(pushNextPage) forControlEvents:UIControlEventTouchUpInside];
23     nextBT.layer.cornerRadius = 8;
24     [self.view addSubview:nextBT];
25 
26     self.label = [[UILabel alloc] init];
27     self.label.frame = CGRectMake((self.view.frame.size.width - 200)*0.5, 160, 200, 45);
28     self.label.layer.masksToBounds = YES;
29     self.label.layer.cornerRadius = 8.0f;
30     self.label.textColor = [UIColor whiteColor];
31     self.label.backgroundColor = [UIColor blackColor];
32     [self.view addSubview:self.label];
33 
34 }
35 
36 -(void)pushNextPage{
37 
38     __weak ViewController *myself = self;
39 
40     SecondViewController *secVC = [[SecondViewController alloc] init];
41 
42     //------------------------------ 属性传值 ---------------------------------------
43     // 步骤 2:书写 block 的具体实现
44     // 将 Label 的赋值操作放到 block 内实现,等到调用的时候执行(在 SecondVC 的 dealloc 里调用)
45     // 其实就是利用了 block 特性:声明与赋值只是保存了一段代码段,必须调用才能执行内部代码
46     secVC.tbSVC = ^(NSString *textString) {
47 
48         myself.label.text = textString;
49     };
50 
51     [self.navigationController pushViewController:secVC animated:YES];
52 
53     //------------------------------- 参数传值 ----------------------------------
54     // 步骤 2:block 的具体实现,将 Label 的赋值操作放到 block 内实现
55     [secVC getTextStringWithBlock:^(NSString *textString) {
56 
57         NSLog(@"getTextStringWithBlock 代码块:%@",textString);
58         myself.label.text = textString;
59     }];
60 }
61 
62 @end

// - SecondViewController.h

 1 #import 
 2 // 先搞一个 block,用作传值
 3 typedef void(^TextBlockSecVC)(NSString *textString);
 4 
 5 @interface SecondViewController : UIViewController
 6 
 7 /** 属性传值步骤
 8 
 9    1 在传值页面里(SecondViewController)声明 block 属性
10    2 在被传值的页面中(ViewController)写 block 的具体实现(赋值)
11    3 在传值页面里调用 block 进行传值
12 */
13 // 属性传值步骤 1:声明属性
14 @property (strong,nonatomic)TextBlockSecVC tbSVC;
15 
16 /** block 用作参数步骤
17 
18    1 在传值页面里声明属性,同时声明且实现自带 block 参数的方法
19    2 老样子,同样在被传值页面中实现 block
20    3 在传值页面里调用 block(传值)
21 */
22 // block 作为参数传值:步骤 1
23 -(void)getTextStringWithBlock:(TextBlockSecVC )block;
24 
25 @end

// - SecondViewController.m 

 1 #import "SecondViewController.h"
 2 
 3 @interface SecondViewController ()
 4 @property(nonatomic,strong)UITextField *valueTF;// 存放将要传出的值
 5 @end
 6 
 7 @implementation SecondViewController
 8 
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11     self.view.backgroundColor = [UIColor whiteColor];
12 
13     // TextField 用来输入将要要传送的值
14     self.valueTF = [[UITextField alloc] init];
15     self.valueTF.frame = CGRectMake((self.view.frame.size.width - 210)*0.5, 260, 210, 45);
16     self.valueTF.layer.cornerRadius = 4.0f;
17     self.valueTF.backgroundColor = [UIColor brownColor];
18     self.valueTF.textColor = [UIColor blackColor];
19     self.valueTF.text = @"将要被传值01";
20     [self.view addSubview:self.valueTF];
21 }
22 
23 //// 属性传值
24 //-(void)dealloc{
25 //
26 //   // 属性传值步骤 3:调用传值
27 //   self.tbSVC(self.valueTF.text);
28 //}
29 
30 //====================================================
31 
32 // 参数传值步骤 1:声明、实现方法
33 -(void)getTextStringWithBlock:(TextBlockSecVC)block{
34 
35 //    // 步骤 3 调用传值
36 //    block(@"参数传值");// 方法内部
37     
38     // 步骤 3:调用传值
39     self.tbSVC = block;// 利用属性在方法外部调用
40 }
41 
42 
43 // 参数传值
44 -(void)viewDidDisappear:(BOOL)animated{
45 
46     [super viewDidDisappear:YES];
47     self.tbSVC(self.valueTF.text);// 回调传值
48 }
49 
50 @end

运行效果:secVC 页面中键入 Make  |  ViewController 中展示