页面传值:属性,协议,Block传值

1.属性传值和协议传值

 1 #import "RootViewController.h"
 2 #import "SecondViewController.h"
 3 #warning 第四步:签订协议
 4 @interface RootViewController ()<SecondViewControllerDelegate>
 5 @property(nonatomic,strong)UILabel *showTextLable;
 6 @end
 7 
 8 @implementation RootViewController
 9 
10 - (void)viewDidLoad {
11     [super viewDidLoad];
12     // Do any additional setup after loading the view.
13     self.view.backgroundColor = [UIColor whiteColor];
14     self.navigationItem.title = @"首页";
15     //创建UILable用于显示内容
16     UILabel *showTextLable = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 150, 50)];
17     showTextLable.backgroundColor = [UIColor yellowColor];
18     showTextLable.text = @"美女帅哥";
19     showTextLable.textAlignment = NSTextAlignmentCenter;
20     [self.view addSubview:showTextLable];
21     self.showTextLable = showTextLable;
22     
23     //创建推出下个界面的UIButton
24     UIButton *pushNextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
25     pushNextBtn.frame = CGRectMake(100, 300, 150, 50);
26     pushNextBtn.backgroundColor = [UIColor magentaColor];
27     [pushNextBtn setTitle:@"下一页" forState:UIControlStateNormal];
28     [pushNextBtn addTarget:self action:@selector(pushNextController) forControlEvents:UIControlEventTouchUpInside];
29     [self.view addSubview:pushNextBtn];
30     
31     
32 }
33 -(void)pushNextController
34 {
35     SecondViewController *secondVC = [[SecondViewController alloc] init];
36     //需要把lable的text传给
37     //下面这种传值方法是错误的,因为textField还没有创建,还没有加载到视图上,这里是项目期特别容易犯错的地方,必须要考虑视图加载的顺序
38     //secondVC.textField.text = self.showTextLable.text;
39     //正确的写法应该是直接传送文本内容
40     secondVC.content = self.showTextLable.text;
41     //
42 #warning 第五步:指定代理人
43     secondVC.secondViewControllerDelegate = self;//指定协议代理人
44     [self.navigationController pushViewController:secondVC animated:YES];
45 }
46 #warning 第六步:实现协议的方法
47 -(void)changeValue:(NSString *)text
48 {
49     self.showTextLable.text = text;
50 }
51 - (void)didReceiveMemoryWarning {
52     [super didReceiveMemoryWarning];
53     // Dispose of any resources that can be recreated.
54 }
 1 #import <UIKit/UIKit.h>
 2 #warning 第一步:声明协议
 3 @protocol SecondViewControllerDelegate <NSObject>
 4 
 5 -(void)changeValue:(NSString *)text;
 6 @optional//可选的协议方法
 7 -(void)changeColor:(UIColor *)color;
 8 
 9 @end
10 @interface SecondViewController : UIViewController
11 /**
12  *  显示文本输入框的
13  */
14 @property (nonatomic,strong) UITextField *textField;
15 /**
16  *  显示文本输入框的内容文本
17  */
18 @property (nonatomic,strong) NSString *content;
19 #warning 第二步:声明代理人
20 /**
21  *  第二个Controller中的协议,
22     assign避免循环引用
23  */
24 @property (nonatomic,assign)id<SecondViewControllerDelegate>secondViewControllerDelegate;
25 @end
 1 #import "SecondViewController.h"
 2 
 3 @interface SecondViewController ()
 4 
 5 @end
 6 
 7 @implementation SecondViewController
 8 
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11     // Do any additional setup after loading the view.
12     self.view.backgroundColor = [UIColor whiteColor];
13     self.navigationItem.title= @"第二页";
14     //创建输入文本的文本框
15     UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 150, 50)];
16     textField.placeholder = @"请输入文本内容";
17     textField.borderStyle = UITextBorderStyleRoundedRect;
18     [self.view addSubview:textField];
19     self.textField = textField;
20     self.textField.text = self.content;
21     
22     //创建UIButton用于返回上一个界面
23     UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
24     backBtn.frame = CGRectMake(100, 300, 150, 50);
25     [backBtn setTitle:@"返回" forState:UIControlStateNormal];
26     backBtn.backgroundColor = [UIColor blackColor];
27     [backBtn addTarget:self action:@selector(popButtonAction) forControlEvents:UIControlEventTouchUpInside];
28     [self.view addSubview:backBtn];
29     
30     
31 }
32 -(void)popButtonAction
33 {
34 #warning 第三步:执行相关的协议方法
35     [self.secondViewControllerDelegate changeValue:self.textField.text];
36     [self.navigationController popViewControllerAnimated:YES];
37 }
38 //总结:属性传值:一般使用从前往后进行传值的情况
39 //如果从后往前,需要借助其他传值方式
40 //协议传值方式
41 /**
42  *  实现协议的六个步骤:
43  第一步:声明协议
44  第二步:声明代理人
45  第三步:执行协议方法
46  第四步:签订协议
47  第五步:指定代理人
48  第六步:实现协议方法
49  */

2.Block传值

 1 #import "RootViewController.h"
 2 #import "SecondViewController.h"
 3 @interface RootViewController ()
 4 @property(nonatomic,strong)UILabel *showTextLable;
 5 @end
 6 
 7 @implementation RootViewController
 8 
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11     // Do any additional setup after loading the view.
12     UILabel *showLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 150, 50)];
13     showLabel.backgroundColor = [UIColor redColor];
14     showLabel.text = @"美女帅哥";
15     showLabel.textAlignment = NSTextAlignmentCenter;
16     showLabel.font = [UIFont systemFontOfSize:24];
17     [self.view addSubview:showLabel];
18     self.showTextLable = showLabel;
19     UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
20     nextBtn.frame = CGRectMake(100, 300, 150, 50);
21     nextBtn.backgroundColor = [UIColor yellowColor];
22     [nextBtn setTitle:@"下一页" forState:UIControlStateNormal];
23     [nextBtn addTarget:self action:@selector(showNextAction) forControlEvents:UIControlEventTouchUpInside];
24     [self.view addSubview:nextBtn];
25     
26 }
27 -(void)showNextAction
28 {
29     SecondViewController *secondView = [[SecondViewController alloc] init];
30     [self.navigationController pushViewController:secondView animated:YES];
31     //ARC模式下用__weak修饰可解决内存泄漏问题,MRC模式下用__block
32     __weak typeof (self)weakSelf = self;
33 #warning 第三步:实现Block的内容(实现传递过来的内容)
34     secondView.changeValueBlock = ^(NSString *string){
35         //下面这种引用容易造成内存泄漏
36         //self.showTextLable.text = string;
37         weakSelf.showTextLable.text = string;
38     };
39     //block的核心是:回调,Block作为属性进行传值的时候,一般使用场景是从第二个界面返回到上一个界面,基本步骤:
40     //1.在第二个界面中声明一个Block属性
41     //2.在第二个界面返回的那句代码之前加上Block需要传递的内容
42     //3.在一个界面推出第二个界面那句代码之前,对Block进行实现(即接受Block传递过来的内容)
43      secondView.content = self.showTextLable.text;
44 }
1 #import <UIKit/UIKit.h>
2 
3 @interface SecondViewController : UIViewController
4 @property (nonatomic,strong) NSString *content;
5 @property (strong,nonatomic) UITextField *textField;
6 #warning 第一步:声明Block属性,copy是为了解决循环引用的问题
7 @property (nonatomic,copy) void(^changeValueBlock)(NSString *string);
8 @end
 1 #import "SecondViewController.h"
 2 
 3 @interface SecondViewController ()
 4 
 5 @end
 6 
 7 @implementation SecondViewController
 8 
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11     // Do any additional setup after loading the view.
12     self.view.backgroundColor = [UIColor whiteColor];
13     self.view.backgroundColor = [UIColor whiteColor];
14     self.navigationItem.title= @"第二页";
15     //创建输入文本的文本框
16     UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 150, 50)];
17     textField.placeholder = @"请输入文本内容";
18     textField.borderStyle = UITextBorderStyleRoundedRect;
19     [self.view addSubview:textField];
20     self.textField = textField;
21     self.textField.text = self.content;
22     
23     //创建UIButton用于返回上一个界面
24     UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
25     backBtn.frame = CGRectMake(100, 300, 150, 50);
26     [backBtn setTitle:@"返回" forState:UIControlStateNormal];
27     backBtn.backgroundColor = [UIColor blackColor];
28     [backBtn addTarget:self action:@selector(popButtonAction) forControlEvents:UIControlEventTouchUpInside];
29     [self.view addSubview:backBtn];
30 }
31 -(void)popButtonAction
32 {
33 #warning 第二步:将要传递的内容通过属性block先给它
34     self.changeValueBlock(self.textField.text);
35   
36     [self.navigationController popViewControllerAnimated:YES];
37 }

 

转载于:https://www.cnblogs.com/DevinSMR/p/5218153.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值