页面传值是指:父子页面之间、非父子页面之间、兄弟页面之间、非兄弟页面之间数据互通的方式,是为页面传值(个人见解)
这里我所整理的传值方式有六个,分别是:
1、属性传值
2、单例传值
3、NSUserDefaults传值
4、代理传值
5、Block传值
6、通知传值
1. 属性传值
传值描述:
A页面、B页面
A页面在跳转之前赋值给B页面声明的全局变量(可以是数组、字典)-》B页面声明全局变量(可以是数组、字典),并接收A页面传来的值
实现代码步骤:
1.在B页面中声明全局变量,以便在其他页面中调用,同时使用全局变量赋值给UI控件
2.在A页面中跳转之前,A页面所传的值通过赋值给B页面属性进行传值
直接上代码:
A页面代码:
#import "AViewController.h"
//导入B页面
#import "A-2-ViewController.h"
@interface AViewController ()
//声明全局指针,UIButton、UITextField
@property (nonatomic,strong) UIButton *btn;
@property (nonatomic,strong) UITextField *textF;
@end
@implementation AViewController
//UITextField的懒加载
- (UITextField *)textF
{
if (_textF == nil) {
_textF = [[UITextField alloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, 100, 200, 40)];
_textF.textColor = [UIColor blackColor];
_textF.borderStyle = UITextBorderStyleLine;
}
return _textF;
}
//UIButton的懒加载
- (UIButton *)btn
{
if (_btn == nil) {
_btn = [UIButton buttonWithType:UIButtonTypeCustom];
_btn.backgroundColor = [UIColor redColor];
_btn.frame = CGRectMake((self.view.frame.size.width-200)/2, _textF.frame.origin.y+_textF.frame.size.height+40, 200, 40);
[_btn setTitle:@"跳转页面二" forState:UIControlStateNormal];
[_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//点击事件
[_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
}
return _btn;
}
- (void)viewDidLoad {
[super viewDidLoad];
//把UIButton、UITextField 添加到view视图上
[self.view addSubview:self.textF];
[self.view addSubview:self.btn];
}
//button点击事件 -- 跳转到B页面
- (void)btnClick
{
//创建B页面指针,并初始化
A_2_ViewController *a_2_VC = [[A_2_ViewController alloc]init];
//属性传值 - 输入要传的值
a_2_VC.str = self.textF.text;
//设置背景色
a_2_VC.view.backgroundColor = [UIColor whiteColor];
//跳转到下级页面
[self presentViewController:a_2_VC animated:YES completion:nil];
}
@end
B页面代码:
A-2-ViewController.h
#import <UIKit/UIKit.h>
@interface A_2_ViewController : UIViewController
@property (nonatomic, strong) NSString *str;
@end
A-2-ViewController.m
#import "A-2-ViewController.h"
@interface A_2_ViewController ()
//声明全局指针,UIButton、UILabel
@property (nonatomic,strong) UILabel *label;
@property (nonatomic,strong) UIButton *btn;
@end
@implementation A_2_ViewController
//UILabel的懒加载
- (UILabel *)label
{
if (_label == nil) {
_label = [[UILabel alloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, 100, 200, 40)];
_label.backgroundColor = [UIColor blackColor];
_label.textColor = [UIColor whiteColor];
_label.font = [UIFont systemFontOfSize:20];
//属性传值 - 接收传值
_label.text = self.str;
}
return _label;
}
//UIButton的懒加载
- (UIButton *)btn
{
if (_btn == nil) {
_btn = [UIButton buttonWithType:UIButtonTypeCustom];
_btn.backgroundColor = [UIColor redColor];
_btn.frame = CGRectMake((self.view.frame.size.width-200)/2, _label.frame.origin.y+_label.frame.size.height+40, 200, 40);
[_btn setTitle:@"跳转页面一" forState:UIControlStateNormal];
[_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_btn.titleLabel.font = [UIFont systemFontOfSize:20];
[_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
}
return _btn;
}
- (void)viewDidLoad {
[super viewDidLoad];
//把UIButton、UILabel 添加到view视图上
[self.view addSubview:self.label];
[self.view addSubview:self.btn];
}
//返回A页面
- (void)btnClick
{
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
2. 单例传值
传值描述:
A页面、B页面、X单例类
A页面-》B页面-》B页面给X单例类属性赋值-》A页面从X单例类属性获取所传的值
实现代码步骤:
1.先创建一个单例类,声明好所需的属性
2.在B页面调用单例类的属性,并给单例类属性赋值
3.在A页面中使用 viewWillAppear() 生命周期里,使用单例类属性,给UI控件赋值
直接上代码:
单例类代码:
DefaultInstance.h
#import <Foundation/Foundation.h>
@interface DefaultInstance : NSObject
@property (nonatomic,strong) NSString *str;
//类方法
+ (instancetype)shatredInstance;
@end
DefaultInstance.m
#import "DefaultInstance.h"
@implementation DefaultInstance
//通过类方法创建爱你单例对象
+ (instancetype)shatredInstance
{
static DefaultInstance *sharedVC = nil;
if (sharedVC == nil) {
sharedVC = [[DefaultInstance alloc]init];
}
return sharedVC;
}
@end
A页面代码:
#import "BViewController.h"
//导入B页面、单例类
#import "B-2-ViewController.h"
#import "DefaultInstance.h"
@interface BViewController ()
//声明全局指针,UILabelUIButton
@property (nonatomic,strong) UILabel *tsLabel;
@property (nonatomic,strong) UILabel *btLabel;
@property (nonatomic,strong) UITextField *textF;
@property (nonatomic,strong) UIButton *btn;
@property (nonatomic,strong) UILabel *tsLabel_01;
@end
@implementation BViewController
//UILabel的懒加载
- (UILabel *)tsLabel
{
if (_tsLabel == nil) {
_tsLabel = [[UILabel alloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, 120, 200, 40)];
_tsLabel.backgroundColor = [UIColor darkGrayColor];
_tsLabel.textColor = [UIColor whiteColor];
_tsLabel.font = [UIFont systemFontOfSize:20];
_tsLabel.text = @"正向传值";
_tsLabel.textColor = [UIColor whiteColor];
_tsLabel.textAlignment = NSTextAlignmentCenter;
}
return _tsLabel;
}
//UILabel的懒加载
- (UILabel *)btLabel
{
if (_btLabel == nil) {
_btLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, (self.tsLabel.frame.origin.y+self.tsLabel.frame.size.height)+50, 150, 40)];
_btLabel.backgroundColor = [UIColor grayColor];
_btLabel.textColor = [UIColor whiteColor];
_btLabel.font = [UIFont systemFontOfSize:20];
_btLabel.text = @"发送传值内容:";
}
return _btLabel;
}
//UITextField的懒加载
- (UITextField *)textF
{
if (_textF == nil) {
_textF = [[UITextField alloc]initWithFrame:CGRectMake((self.btLabel.frame.origin.x+self.btLabel.frame.size.width), (self.tsLabel.frame.origin.y+self.tsLabel.frame.size.height)+50, 200, 40)];
_textF.textColor = [UIColor blackColor];
_textF.borderStyle = UITextBorderStyleLine;
// _textF.backgroundColor = [UIColor grayColor];
_textF.placeholder = @"请输入内容";
}
return _textF;
}
//UIButton的懒加载
- (UIButton *)btn
{
if (_btn == nil) {
_btn = [UIButton buttonWithType:UIButtonTypeCustom];
_btn.backgroundColor = [UIColor redColor];
_btn.frame = CGRectMake((self.view.frame.size.width-200)/2, _textF.frame.origin.y+_textF.frame.size.height+40, 200, 40);
[_btn setTitle:@"跳转B页面" forState:UIControlStateNormal];
[_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//点击事件
[_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
}
return _btn;
}
//UILabel的懒加载
- (UILabel *)tsLabel_01
{
if (_tsLabel_01 == nil) {
_tsLabel_01 = [[UILabel alloc]init];
// _tsLabel_01.backgroundColor = [UIColor darkGrayColor];
_tsLabel_01.textColor = [UIColor redColor];
_tsLabel_01.text = @"* 注:\n\t 由于是模态跳转页面,所以只能正向传值,不能方向传值,因为反向传值在模态退出后,A页面的每一个生命周期都不会被触发,从而不能获取单例类属性传值,目前我没有找到在模态下可以反向传值的方法,如果你有相应的方法会,你可以在简书上给我留言:\n https://www.jianshu.com/p/271e83234273\n网址到代码里复制!";
NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:14]};
CGSize labelSize = [_tsLabel_01.text boundingRectWithSize:CGSizeMake(200, 5000) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
_tsLabel_01.frame = CGRectMake(10, (_btn.frame.origin.y+_btn.frame.size.height)+20,self.view.frame.size.width-20, labelSize.height+70);
_tsLabel_01.numberOfLines = 0;
_tsLabel_01.lineBreakMode = NSLineBreakByWordWrapping;
_tsLabel_01.font = [UIFont systemFontOfSize:20];
_tsLabel_01.textAlignment = NSTextAlignmentLeft;
}
return _tsLabel_01;
}
- (void)viewDidLoad {
[super viewDidLoad];
//把UILabel、UIButton添加到view视图中_btLabel
[self.view addSubview:self.tsLabel];
[self.view addSubview:self.btLabel];
[self.view addSubview:self.textF];
[self.view addSubview:self.btn];
[self.view addSubview:self.tsLabel_01];
}
//button点击事件 -- 跳转到页面二
- (void)btnClick
{
//创建B页面指针,并初始化
B_2_ViewController *b_2_VC = [[B_2_ViewController alloc]init];
b_2_VC.view.backgroundColor = [UIColor whiteColor];
/*
单例传值 - 正向传递
通过单例类向B页面传值
*/
[DefaultInstance shatredInstance].str = _textF.text;
//跳转下级页面
[self presentViewController:b_2_VC animated:YES completion:nil];
}
@end
B页面代码:
#import "B-2-ViewController.h"
//导入单例类
#import "DefaultInstance.h"
@interface B_2_ViewController ()
//声明全局指针,UITextField、UIButton
@property (nonatomic,strong) UILabel *label;
@property (nonatomic,strong) UIButton *btn;
@end
@implementation B_2_ViewController
//UILabel的懒加载
- (UILabel *)label
{
if (_label == nil) {
_label = [[UILabel alloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, 100, 200, 40)];
_label.backgroundColor = [UIColor blackColor];
_label.textColor = [UIColor whiteColor];
_label.font = [UIFont systemFontOfSize:20];
}
return _label;
}
//UIButton的懒加载
- (UIButton *)btn
{
if (_btn == nil) {
_btn = [UIButton buttonWithType:UIButtonTypeCustom];
_btn.backgroundColor = [UIColor redColor];
_btn.frame = CGRectMake((self.view.frame.size.width-200)/2, _label.frame.origin.y+_label.frame.size.height+40, 200, 40);
[_btn setTitle:@"返回A页面" forState:UIControlStateNormal];
[_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_btn.titleLabel.font = [UIFont systemFontOfSize:20];
[_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
}
return _btn;
}
- (void)viewDidLoad {
[super viewDidLoad];
//把UITextField、UIButton添加到view视图上
[self.view addSubview:self.label];
[self.view addSubview:self.btn];
}
//视图即将显示
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//通过单例类属性获取A页面传值,并赋值给字符串指针
NSString *str = [DefaultInstance shatredInstance].str;
//给B页面的标签赋值
self.label.text = str;
}
//返回A页面
- (void)btnClick
{
//返回A页面
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
@end
3. NSUserDefaults传值
传值描述:
A页面、B页面
A页面-》B页面-》B页面使用UserDefaults类存储需要传的值-》A页面接收
实现代码步骤:
1.在B页面中返回上级页面之前,通过UserDefaults来存储需要传的值
2.在A页面中的生命周期 viewWillAppear() 里,通过 UserDefaults 内存的key来获取值
注:UserDefaults里存有String(字符串)、Array(数组)、Dictionary(字典),这里需要注意的是在获取值的时候,如果要获取数组,那么使用时是UserDefaults().array() 。如果是字典,那么使用时是UserDefaults(). dictionary() 。
实现代码步骤:
直接上代码:
A页面代码:
#import "CViewController.h"
//导入B页面的.h类名
#import "C-2-ViewController.h"
@interface CViewController ()
//声明全局UI指针
@property (nonatomic,strong) UILabel *label;
@property (nonatomic,strong) UIButton *btn;
@end
@implementation CViewController
//UILabel的懒加载
- (UILabel *)label
{
if (_label == nil) {
_label = [[UILabel alloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, 100, 200, 40)];
_label.backgroundColor = [UIColor blackColor];
_label.textColor = [UIColor whiteColor];
_label.font = [UIFont systemFontOfSize:20];
}
return _label;
}
//UIButton的懒加载
- (UIButton *)btn
{
if (_btn == nil) {
_btn = [UIButton buttonWithType:UIButtonTypeCustom];
_btn.backgroundColor = [UIColor redColor];
_btn.frame = CGRectMake((self.view.frame.size.width-200)/2, _label.frame.origin.y+_label.frame.size.height+40, 200, 40);
[_btn setTitle:@"跳转页面二" forState:UIControlStateNormal];
[_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//点击事件
[_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
}
return _btn;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//把UI指针添加到View视图中
[self.view addSubview:self.label];
[self.view addSubview:self.btn];
}
//button点击事件 -- 跳转到B页面
- (void)btnClick
{
//创建类指针,并初始化
C_2_ViewController *c_2_VC = [[C_2_ViewController alloc]init];
//跳转到B页面
[self.navigationController pushViewController:c_2_VC animated:YES ];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//接收B页面 - 反向传值
//如果在接收传值后,发现已经有值了,不要紧张这是存储一次,每次运行都会显示这个值。如果你不想已经页面就显示,那么可以先在赋值前做一个判断,如果key中有值就先清除,然后在进到页面二中存储一个,然后在返回到页面一
self.label.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"NSUserDefaults-re"];
}
@end
B页面代码:
#import "C-2-ViewController.h"
@interface C_2_ViewController ()
//声明全局UI指针
@property (nonatomic,strong) UITextField *textF;
@property (nonatomic,strong) UIButton *btn;
@end
@implementation C_2_ViewController
//UITextField的懒加载
- (UITextField *)textF
{
if (_textF == nil) {
_textF = [[UITextField alloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, 100, 200, 40)];
_textF.textColor = [UIColor blackColor];
_textF.borderStyle = UITextBorderStyleLine;
}
return _textF;
}
//UIButton的懒加载
- (UIButton *)btn
{
if (_btn == nil) {
_btn = [UIButton buttonWithType:UIButtonTypeCustom];
_btn.backgroundColor = [UIColor redColor];
_btn.frame = CGRectMake((self.view.frame.size.width-200)/2, _textF.frame.origin.y+_textF.frame.size.height+40, 200, 40);
[_btn setTitle:@"跳转页面一" forState:UIControlStateNormal];
[_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_btn.titleLabel.font = [UIFont systemFontOfSize:20];
[_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
}
return _btn;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//把UI指针添加到View视图中
[self.view addSubview:self.textF];
[self.view addSubview:self.btn];
}
//返回A页面
- (void)btnClick
{
//把想要传递的值写入文件中
[[NSUserDefaults standardUserDefaults] setObject:self.textF.text forKey:@"NSUserDefaults-re"];
//接着要把NSUserDefaults同步一下
[[NSUserDefaults standardUserDefaults]synchronize];
//返回A页面
[self.navigationController popViewControllerAnimated:YES];
}
@end
4. 代理传值
传值描述:
A页面、B页面
A页面-》B页面-》B页面通过代理协议-》A页面
实现代码步骤:
1.在B页面中声明代理协议
2.在B页面中实现代理协议
3.在B页面中返回上级页面之前,使用代理协议赋值给参数
4.在A页面中跳转下级页面之前,设置B页面的代理协议
5.在A页面中实现B页面的代理协议,并在代理协议中通过参数获取传值,然后赋值给UI控件
实现代码步骤:
直接上代码:
A页面代码:
#import "DViewController.h"
//导入B页面类名
#import "D-2-ViewController.h"
//添加代理协议
@interface DViewController ()<passValueDelegate>
//声明全局UI指针
@property (nonatomic,strong) UILabel *label;
@property (nonatomic,strong) UIButton *btn;
@end
@implementation DViewController
//UILabel的懒加载
- (UILabel *)label
{
if (_label == nil) {
_label = [[UILabel alloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, 100, 200, 40)];
_label.backgroundColor = [UIColor blackColor];
_label.textColor = [UIColor whiteColor];
_label.font = [UIFont systemFontOfSize:20];
}
return _label;
}
//UIButton的懒加载
- (UIButton *)btn
{
if (_btn == nil) {
_btn = [UIButton buttonWithType:UIButtonTypeCustom];
_btn.backgroundColor = [UIColor redColor];
_btn.frame = CGRectMake((self.view.frame.size.width-200)/2, _label.frame.origin.y+_label.frame.size.height+40, 200, 40);
[_btn setTitle:@"跳转B页面" forState:UIControlStateNormal];
[_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//点击事件
[_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
}
return _btn;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//把UI指针添加到View视图中
[self.view addSubview:self.label];
[self.view addSubview:self.btn];
}
//button点击事件 -- 跳转到B页面
- (void)btnClick
{
//创建指针,并初始化
D_2_ViewController *d_2_VC = [[D_2_ViewController alloc]init];
//设置代理
d_2_VC.delegate = self;
//进入模态页面
[self presentViewController:d_2_VC animated:YES completion:nil];
}
//代理传值 - 实现协议方法 - 接收来自B页面传值
- (void)passValue:(NSString *)str
{
if (str) {
//把获取到的值,赋值给标签
self.label.text = str;
}
}
@end
B页面代码:
B页面 .h 代码
#import <UIKit/UIKit.h>
//委托方 - 创建一个协议
@protocol passValueDelegate <NSObject>
//协议定义一个传值的方法
- (void)passValue:(NSString *)str;
@end
@interface D_2_ViewController : UIViewController
//声明全局代理协议
@property (weak) id<passValueDelegate>delegate;
@end
B页面 .m 代码
#import "D-2-ViewController.h"
@interface D_2_ViewController ()
//声明全局UI指针
@property (nonatomic,strong) UITextField *textF;
@property (nonatomic,strong) UIButton *btn;
@end
@implementation D_2_ViewController
//UITextField的懒加载
- (UITextField *)textF
{
if (_textF == nil) {
_textF = [[UITextField alloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, 100, 200, 40)];
_textF.textColor = [UIColor blackColor];
_textF.borderStyle = UITextBorderStyleLine;
}
return _textF;
}
//UIButton的懒加载
- (UIButton *)btn
{
if (_btn == nil) {
_btn = [UIButton buttonWithType:UIButtonTypeCustom];
_btn.backgroundColor = [UIColor redColor];
_btn.frame = CGRectMake((self.view.frame.size.width-200)/2, _textF.frame.origin.y+_textF.frame.size.height+40, 200, 40);
[_btn setTitle:@"返回B页面" forState:UIControlStateNormal];
[_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_btn.titleLabel.font = [UIFont systemFontOfSize:20];
[_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
}
return _btn;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//把UI指针添加到View视图中
[self.view addSubview:self.textF];
[self.view addSubview:self.btn];
}
//返回页面一
- (void)btnClick
{
//代理传值 - 反向传值
[self.delegate passValue:self.textF.text];
//退出模态页面
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
5. Block传值
传值描述:
A页面、B页面
A页面-》B页面-》B页面声明闭包(Block),并返回A页面的同时使用闭包传值(Block)-》A页面获取传值
实现代码步骤:
1.在B页面中声明全局的闭包(Block),并设置好要参数类型
2.在B页面中使用闭包(Block)赋值给闭包的参数
3.在A页面中在跳转页面之前,使用B页面的闭包给A页面的标签赋值
直接上代码:
A页面代码:
#import "EViewController.h"
//导入B页面头文件
#import "E-2-ViewController.h"
@interface EViewController ()
//声明全局UI指针
@property (nonatomic,strong) UILabel *label;
@property (nonatomic,strong) UIButton *btn;
@end
@implementation EViewController
//UILabel的懒加载
- (UILabel *)label
{
if (_label == nil) {
_label = [[UILabel alloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, 100, 200, 40)];
_label.backgroundColor = [UIColor blackColor];
_label.textColor = [UIColor whiteColor];
_label.font = [UIFont systemFontOfSize:20];
}
return _label;
}
//UIButton的懒加载
- (UIButton *)btn
{
if (_btn == nil) {
_btn = [UIButton buttonWithType:UIButtonTypeCustom];
_btn.backgroundColor = [UIColor redColor];
_btn.frame = CGRectMake((self.view.frame.size.width-200)/2, _label.frame.origin.y+_label.frame.size.height+40, 200, 40);
[_btn setTitle:@"跳转B页面" forState:UIControlStateNormal];
[_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//点击事件
[_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
}
return _btn;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//把UI指针添加到View视图找那个
[self.view addSubview:self.label];
[self.view addSubview:self.btn];
}
//button点击事件 -- 跳转到B页面
- (void)btnClick
{
//声明指针,并初始化
E_2_ViewController *e_2_VC = [[E_2_ViewController alloc]init];
//block传值 - 实现block - 接收来自B页面传值
e_2_VC.block = ^(NSString *str) {
self.label.text = str;
};
//模态进入B页面
[self presentViewController:e_2_VC animated:YES completion:nil];
}
@end
B页面代码:
B页面 .h 代码:
#import <UIKit/UIKit.h>
@interface E_2_ViewController : UIViewController
//定义一个block进行页面反向传值;在创建的时候,要使用copy,这是为了循环使用
@property (copy) void (^block)(NSString *);
@end
B页面 .m 代码:
#import "E-2-ViewController.h"
@interface E_2_ViewController ()
//声明全局UI指针
@property (nonatomic,strong) UITextField *textF;
@property (nonatomic,strong) UIButton *btn;
@end
@implementation E_2_ViewController
//UITextField的懒加载
- (UITextField *)textF
{
if (_textF == nil) {
_textF = [[UITextField alloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, 100, 200, 40)];
_textF.textColor = [UIColor blackColor];
_textF.borderStyle = UITextBorderStyleLine;
}
return _textF;
}
//UIButton的懒加载
- (UIButton *)btn
{
if (_btn == nil) {
_btn = [UIButton buttonWithType:UIButtonTypeCustom];
_btn.backgroundColor = [UIColor redColor];
_btn.frame = CGRectMake((self.view.frame.size.width-200)/2, _textF.frame.origin.y+_textF.frame.size.height+40, 200, 40);
[_btn setTitle:@"返回A页面" forState:UIControlStateNormal];
[_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_btn.titleLabel.font = [UIFont systemFontOfSize:20];
[_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
}
return _btn;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//把UI指针添加到View视图中
[self.view addSubview:self.textF];
[self.view addSubview:self.btn];
// Do any additional setup after loading the view.
}
//返回A页面
- (void)btnClick
{
//block传值 - 反向传值
self.block(self.textF.text);
//退出模态页面
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
2. 通知传值
传值描述:
A页面、B页面
A页面-》B页面-》B页面使用通知(NotificationCenter)传值-》A页面使用通知(NotificationCenter)获取传值
实现代码步骤:
1.在B页面中使用通知中心(NotificationCenter)调用post去赋值,同时发送通知消息
2.在B页面中需要实现“移除通知”,必须实现这个代码
3.在A页面中使用通知中心(NotificationCenter)调用addObserver来设置通知的监听事件
4.实现通知中心(NotificationCenter)的监听事件,并在方法里获取B页面传过来的值
注:注册通知中心的“Name”值,在A、B页面中通知中心的“Name”值必须一致,否则获取不到传值
直接上代码:
A页面代码:
#import "FViewController.h"
//导入B页面头文件
#import "F-2-ViewController.h"
@interface FViewController ()
//声明全局UI指针
@property (nonatomic,strong) UILabel *label;
@property (nonatomic,strong) UIButton *btn;
@end
@implementation FViewController
//UILabel的懒加载
- (UILabel *)label
{
if (_label == nil) {
_label = [[UILabel alloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, 100, 200, 40)];
_label.backgroundColor = [UIColor blackColor];
_label.textColor = [UIColor whiteColor];
_label.font = [UIFont systemFontOfSize:20];
}
return _label;
}
//UIButton的懒加载
- (UIButton *)btn
{
if (_btn == nil) {
_btn = [UIButton buttonWithType:UIButtonTypeCustom];
_btn.backgroundColor = [UIColor redColor];
_btn.frame = CGRectMake((self.view.frame.size.width-200)/2, _label.frame.origin.y+_label.frame.size.height+40, 200, 40);
[_btn setTitle:@"跳转B页面" forState:UIControlStateNormal];
[_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//点击事件
[_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
}
return _btn;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//把UI指针添加到View视图中
[self.view addSubview:self.label];
[self.view addSubview:self.btn];
}
//button点击事件 -- 跳转到页面二
- (void)btnClick
{
//声明指针,并初始化
F_2_ViewController *f_2_VC = [[F_2_ViewController alloc]init];
//通知传值 - 添加监听等待页面二的传值
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notHandle:) name:@"notify" object:nil];
//跳转B页面
[self.navigationController pushViewController:f_2_VC animated:YES];
// [self presentViewController:f_2_VC animated:YES completion:nil];
}
//接收到通知之后的处理 - 参数1:通知
- (void)notHandle:(NSNotification *)not
{
//把获取的传值,赋值给标签
self.label.text = not.userInfo[@"not"];
}
@end
B页面代码:
#import "F-2-ViewController.h"
@interface F_2_ViewController ()
//声明全局UI指针
@property (nonatomic,strong) UITextField *textF;
@property (nonatomic,strong) UIButton *btn;
@end
@implementation F_2_ViewController
//UITextField的懒加载
- (UITextField *)textF
{
if (_textF == nil) {
_textF = [[UITextField alloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, 100, 200, 40)];
_textF.textColor = [UIColor blackColor];
_textF.borderStyle = UITextBorderStyleLine;
}
return _textF;
}
//UIButton的懒加载
- (UIButton *)btn
{
if (_btn == nil) {
_btn = [UIButton buttonWithType:UIButtonTypeCustom];
_btn.backgroundColor = [UIColor redColor];
_btn.frame = CGRectMake((self.view.frame.size.width-200)/2, _textF.frame.origin.y+_textF.frame.size.height+40, 200, 40);
[_btn setTitle:@"返回A页面" forState:UIControlStateNormal];
[_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_btn.titleLabel.font = [UIFont systemFontOfSize:20];
[_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
}
return _btn;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//把UI指针添加到View视图中
[self.view addSubview:self.textF];
[self.view addSubview:self.btn];
}
//返回A页面
- (void)btnClick
{
//通知传值 - 发送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"notify" object:nil userInfo:@{@"not":self.textF.text}];
//返回A页面
[self.navigationController popViewControllerAnimated:YES];
}
@end
传值的几种方式,根据自己的需求来使用,其中,有几个可以实现正向传值、反向传值,这里就不再写了,太长了,也太累,原谅我的懒惰吧!
代码请到gitee上下载:https://gitee.com/h8900961/ios-value-transfer-mode
有任何问题,请留言,我会第一时间去处理。
谢谢,浏览!