Block传值

其实block传值个人感觉跟代理很相似.也是从后往前传.
//流程:
1.后一个界面定义一个block,并且定义一个属性block
2.在后一个界面返回前一个界面的瞬间,(即:创建完成一个界面之后),调用block;
3.前一个界面实现block的实现
4.后一个界面在合适的机会, 让(传的值以参数的形式 含在block的参数里)
 
代码如下: 
 1 //
 2 //  ViewController.h
 3 //  Block传值
 4 //
 5 //  Created by luoyin on 15/4/23.
 6 //  Copyright (c) 2015年 luoyin. All rights reserved.
 7 //
 8 
 9 #import <UIKit/UIKit.h>
10 
11 @interface ViewController : UIViewController
12 
13 
14 @end
 1 //
 2 //  ViewController.m
 3 //  Block传值
 4 //
 5 //  Created by luoyin on 15/4/23.
 6 //  Copyright (c) 2015年 luoyin. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 #import "SecondViewController.h"
11 
12 @interface ViewController ()
13 @property (nonatomic, strong) UILabel *label;
14 
15 @end
16 
17 @implementation ViewController
18 
19 - (void)viewDidLoad {
20     [super viewDidLoad];
21     // Do any additional setup after loading the view, typically from a nib.
22     
23     self.navigationItem.title = @"第一个页面";
24     self.view.backgroundColor = [UIColor grayColor];
25     
26     //添加按钮,跳转到下一个页面
27     CGFloat mainscreen = [UIScreen mainScreen].bounds.size.width;
28     UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 160, mainscreen -200, 40)];
29     [button setTitle:@"下一页" forState:UIControlStateNormal];
30     button.backgroundColor = [UIColor blueColor];
31     [button addTarget:self action:@selector(nextView:) forControlEvents:UIControlEventTouchUpInside];
32     [self.view addSubview:button];
33     
34     //创建一个label,显示第二个页面传过来的值
35     _label = [[UILabel alloc]initWithFrame:CGRectMake(40, 100, mainscreen-80, 40)];
36     _label.textAlignment = NSTextAlignmentCenter;
37     _label.text = @"等待第二个页面Block传值";
38     _label.backgroundColor = [UIColor yellowColor];
39     _label.textColor = [UIColor grayColor];
40     [self.view addSubview:_label];
41     
42 }
43 
44 - (void)nextView:(UIButton *)button{
45     
46     button.backgroundColor = [UIColor redColor];
47     SecondViewController *secondView = [[SecondViewController alloc]init];
48     
49     //把第二页输入框输入的字符串,通过block内部实现传过来
50     secondView.block = ^(NSString *string){
51         _label.text = string;
52     };
53     [self.navigationController pushViewController:secondView animated:NO];
54 }
55 
56 - (void)didReceiveMemoryWarning {
57     [super didReceiveMemoryWarning];
58     // Dispose of any resources that can be recreated.
59 }
60 
61 @end
 1 //
 2 //  SecondViewController.h
 3 //  Block传值
 4 //
 5 //  Created by luoyin on 15/4/23.
 6 //  Copyright (c) 2015年 luoyin. All rights reserved.
 7 //
 8 
 9 #import <UIKit/UIKit.h>
10 
11 typedef void(^secondViewBlock)(NSString *string);
12 @interface SecondViewController : UIViewController
13 
14 @property (nonatomic, copy)secondViewBlock block;
15 
16 @end
 1 //
 2 //  SecondViewController.m
 3 //  Block传值
 4 //
 5 //  Created by luoyin on 15/4/23.
 6 //  Copyright (c) 2015年 luoyin. All rights reserved.
 7 //
 8 
 9 #import "SecondViewController.h"
10 
11 @interface SecondViewController ()
12 
13 @property (nonatomic, strong) UITextField *textField;
14 
15 @end
16 
17 @implementation SecondViewController
18 
19 - (void)viewDidLoad {
20     [super viewDidLoad];
21     // Do any additional setup after loading the view.
22     
23     self.navigationItem.title = @"第二个页面";
24     self.view.backgroundColor = [UIColor grayColor];
25     
26     CGFloat mainscreen = [UIScreen mainScreen].bounds.size.width;
27     _textField = [[UITextField alloc]initWithFrame:CGRectMake(40, 100, mainscreen-80, 40)];
28     _textField.placeholder = @"请输入返回第一个页面的内容";
29     _textField.textAlignment = NSTextAlignmentCenter;
30     _textField.backgroundColor = [UIColor whiteColor];
31     [self.view addSubview:_textField];
32     
33     //配置navigationController
34     self.navigationItem.leftBarButtonItem = ({
35         UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
36         [button setTitle:@"返回" forState:UIControlStateNormal];
37         button.tintColor = [UIColor blackColor];
38         [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
39         [button addTarget:self action:@selector(backToFistView:) forControlEvents:UIControlEventTouchUpInside];
40         UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
41         buttonItem;
42     });
43 }
44 
45 - (void)backToFistView:(UIButton *)button{
46     
47     //调用block方法,把_textField.text参数传过去
48     _block(_textField.text);
49     [self.navigationController popViewControllerAnimated:NO];
50 }
51 
52 - (void)didReceiveMemoryWarning {
53     [super didReceiveMemoryWarning];
54     // Dispose of any resources that can be recreated.
55 }
56 
57 @end

 测试结果如下:

                    

posted @ 2015-04-23 15:51  脸大皮厚歌  阅读(130)  评论(0编辑  收藏  举报