頁(yè)面?zhèn)髦凳侵福焊缸禹?yè)面之間盈罐、非父子頁(yè)面之間扮念、兄弟頁(yè)面之間损搬、非兄弟頁(yè)面之間數(shù)據(jù)互通的方式,是為頁(yè)面?zhèn)髦担▊€(gè)人見(jiàn)解)
這里我所整理的傳值方式有六個(gè)扔亥,分別是:
1场躯、屬性傳值
2、單例傳值
3旅挤、NSUserDefaults傳值
4踢关、代理傳值
5、Block傳值
6粘茄、通知傳值
1. 屬性傳值
傳值描述:
A頁(yè)面签舞、B頁(yè)面
A頁(yè)面在跳轉(zhuǎn)之前賦值給B頁(yè)面聲明的全局變量(可以是數(shù)組、字典)-》B頁(yè)面聲明全局變量(可以是數(shù)組柒瓣、字典)儒搭,并接收A頁(yè)面?zhèn)鱽?lái)的值
實(shí)現(xiàn)代碼步驟:
1.在B頁(yè)面中聲明全局變量,以便在其他頁(yè)面中調(diào)用芙贫,同時(shí)使用全局變量賦值給UI控件
2.在A頁(yè)面中跳轉(zhuǎn)之前搂鲫,A頁(yè)面所傳的值通過(guò)賦值給B頁(yè)面屬性進(jìn)行傳值
直接上代碼:
A頁(yè)面代碼:
#import "AViewController.h"
//導(dǎo)入B頁(yè)面
#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:@"跳轉(zhuǎn)頁(yè)面二" forState:UIControlStateNormal];
[_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//點(diǎn)擊事件
[_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點(diǎn)擊事件 -- 跳轉(zhuǎn)到B頁(yè)面
- (void)btnClick
{
//創(chuàng)建B頁(yè)面指針,并初始化
A_2_ViewController *a_2_VC = [[A_2_ViewController alloc]init];
//屬性傳值 - 輸入要傳的值
a_2_VC.str = self.textF.text;
//設(shè)置背景色
a_2_VC.view.backgroundColor = [UIColor whiteColor];
//跳轉(zhuǎn)到下級(jí)頁(yè)面
[self presentViewController:a_2_VC animated:YES completion:nil];
}
@end
B頁(yè)面代碼:
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:@"跳轉(zhuǎn)頁(yè)面一" 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頁(yè)面
- (void)btnClick
{
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
2. 單例傳值
傳值描述:
A頁(yè)面菠劝、B頁(yè)面赊舶、X單例類(lèi)
A頁(yè)面-》B頁(yè)面-》B頁(yè)面給X單例類(lèi)屬性賦值-》A頁(yè)面從X單例類(lèi)屬性獲取所傳的值
實(shí)現(xiàn)代碼步驟:
1.先創(chuàng)建一個(gè)單例類(lèi),聲明好所需的屬性
2.在B頁(yè)面調(diào)用單例類(lèi)的屬性,并給單例類(lèi)屬性賦值
3.在A頁(yè)面中使用 viewWillAppear() 生命周期里笼平,使用單例類(lèi)屬性园骆,給UI控件賦值
直接上代碼:
單例類(lèi)代碼:
DefaultInstance.h
#import <Foundation/Foundation.h>
@interface DefaultInstance : NSObject
@property (nonatomic,strong) NSString *str;
//類(lèi)方法
+ (instancetype)shatredInstance;
@end
DefaultInstance.m
#import "DefaultInstance.h"
@implementation DefaultInstance
//通過(guò)類(lèi)方法創(chuàng)建愛(ài)你單例對(duì)象
+ (instancetype)shatredInstance
{
static DefaultInstance *sharedVC = nil;
if (sharedVC == nil) {
sharedVC = [[DefaultInstance alloc]init];
}
return sharedVC;
}
@end
A頁(yè)面代碼:
#import "BViewController.h"
//導(dǎo)入B頁(yè)面、單例類(lèi)
#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 = @"發(fā)送傳值內(nèi)容:";
}
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 = @"請(qǐng)輸入內(nèi)容";
}
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:@"跳轉(zhuǎn)B頁(yè)面" forState:UIControlStateNormal];
[_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//點(diǎn)擊事件
[_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 由于是模態(tài)跳轉(zhuǎn)頁(yè)面遇伞,所以只能正向傳值,不能方向傳值捶牢,因?yàn)榉聪騻髦翟谀B(tài)退出后,A頁(yè)面的每一個(gè)生命周期都不會(huì)被觸發(fā)巍耗,從而不能獲取單例類(lèi)屬性傳值,目前我沒(méi)有找到在模態(tài)下可以反向傳值的方法亲族,如果你有相應(yīng)的方法會(huì),你可以在簡(jiǎn)書(shū)上給我留言:\n http://www.reibang.com/p/271e83234273\n網(wǎng)址到代碼里復(fù)制!";
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點(diǎn)擊事件 -- 跳轉(zhuǎn)到頁(yè)面二
- (void)btnClick
{
//創(chuàng)建B頁(yè)面指針花墩,并初始化
B_2_ViewController *b_2_VC = [[B_2_ViewController alloc]init];
b_2_VC.view.backgroundColor = [UIColor whiteColor];
/*
單例傳值 - 正向傳遞
通過(guò)單例類(lèi)向B頁(yè)面?zhèn)髦? */
[DefaultInstance shatredInstance].str = _textF.text;
//跳轉(zhuǎn)下級(jí)頁(yè)面
[self presentViewController:b_2_VC animated:YES completion:nil];
}
@end
B頁(yè)面代碼:
#import "B-2-ViewController.h"
//導(dǎo)入單例類(lèi)
#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頁(yè)面" 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];
//通過(guò)單例類(lèi)屬性獲取A頁(yè)面?zhèn)髦担①x值給字符串指針
NSString *str = [DefaultInstance shatredInstance].str;
//給B頁(yè)面的標(biāo)簽賦值
self.label.text = str;
}
//返回A頁(yè)面
- (void)btnClick
{
//返回A頁(yè)面
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
@end
3. NSUserDefaults傳值
傳值描述:
A頁(yè)面兆衅、B頁(yè)面
A頁(yè)面-》B頁(yè)面-》B頁(yè)面使用UserDefaults類(lèi)存儲(chǔ)需要傳的值-》A頁(yè)面接收
實(shí)現(xiàn)代碼步驟:
1.在B頁(yè)面中返回上級(jí)頁(yè)面之前雷袋,通過(guò)UserDefaults來(lái)存儲(chǔ)需要傳的值
2.在A頁(yè)面中的生命周期 viewWillAppear() 里鸠删,通過(guò) UserDefaults 內(nèi)存的key來(lái)獲取值
注:UserDefaults里存有String(字符串)禁添、Array(數(shù)組)、Dictionary(字典),這里需要注意的是在獲取值的時(shí)候疏虫,如果要獲取數(shù)組卧秘,那么使用時(shí)是UserDefaults().array() 翅敌。如果是字典,那么使用時(shí)是UserDefaults(). dictionary() 餐曹。
實(shí)現(xiàn)代碼步驟:
直接上代碼:
A頁(yè)面代碼:
#import "CViewController.h"
//導(dǎo)入B頁(yè)面的.h類(lèi)名
#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:@"跳轉(zhuǎn)頁(yè)面二" forState:UIControlStateNormal];
[_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//點(diǎn)擊事件
[_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點(diǎn)擊事件 -- 跳轉(zhuǎn)到B頁(yè)面
- (void)btnClick
{
//創(chuàng)建類(lèi)指針逛拱,并初始化
C_2_ViewController *c_2_VC = [[C_2_ViewController alloc]init];
//跳轉(zhuǎn)到B頁(yè)面
[self.navigationController pushViewController:c_2_VC animated:YES ];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//接收B頁(yè)面 - 反向傳值
//如果在接收傳值后俱两,發(fā)現(xiàn)已經(jīng)有值了,不要緊張這是存儲(chǔ)一次旁舰,每次運(yùn)行都會(huì)顯示這個(gè)值锋华。如果你不想已經(jīng)頁(yè)面就顯示,那么可以先在賦值前做一個(gè)判斷箭窜,如果key中有值就先清除毯焕,然后在進(jìn)到頁(yè)面二中存儲(chǔ)一個(gè),然后在返回到頁(yè)面一
self.label.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"NSUserDefaults-re"];
}
@end
B頁(yè)面代碼:
#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:@"跳轉(zhuǎn)頁(yè)面一" 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頁(yè)面
- (void)btnClick
{
//把想要傳遞的值寫(xiě)入文件中
[[NSUserDefaults standardUserDefaults] setObject:self.textF.text forKey:@"NSUserDefaults-re"];
//接著要把NSUserDefaults同步一下
[[NSUserDefaults standardUserDefaults]synchronize];
//返回A頁(yè)面
[self.navigationController popViewControllerAnimated:YES];
}
@end
4. 代理傳值
傳值描述:
A頁(yè)面磺樱、B頁(yè)面
A頁(yè)面-》B頁(yè)面-》B頁(yè)面通過(guò)代理協(xié)議-》A頁(yè)面
實(shí)現(xiàn)代碼步驟:
1.在B頁(yè)面中聲明代理協(xié)議
2.在B頁(yè)面中實(shí)現(xiàn)代理協(xié)議
3.在B頁(yè)面中返回上級(jí)頁(yè)面之前纳猫,使用代理協(xié)議賦值給參數(shù)
4.在A頁(yè)面中跳轉(zhuǎn)下級(jí)頁(yè)面之前,設(shè)置B頁(yè)面的代理協(xié)議
5.在A頁(yè)面中實(shí)現(xiàn)B頁(yè)面的代理協(xié)議竹捉,并在代理協(xié)議中通過(guò)參數(shù)獲取傳值芜辕,然后賦值給UI控件
實(shí)現(xiàn)代碼步驟:
直接上代碼:
A頁(yè)面代碼:
#import "DViewController.h"
//導(dǎo)入B頁(yè)面類(lèi)名
#import "D-2-ViewController.h"
//添加代理協(xié)議
@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:@"跳轉(zhuǎn)B頁(yè)面" forState:UIControlStateNormal];
[_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//點(diǎn)擊事件
[_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點(diǎn)擊事件 -- 跳轉(zhuǎn)到B頁(yè)面
- (void)btnClick
{
//創(chuàng)建指針,并初始化
D_2_ViewController *d_2_VC = [[D_2_ViewController alloc]init];
//設(shè)置代理
d_2_VC.delegate = self;
//進(jìn)入模態(tài)頁(yè)面
[self presentViewController:d_2_VC animated:YES completion:nil];
}
//代理傳值 - 實(shí)現(xiàn)協(xié)議方法 - 接收來(lái)自B頁(yè)面?zhèn)髦?- (void)passValue:(NSString *)str
{
if (str) {
//把獲取到的值块差,賦值給標(biāo)簽
self.label.text = str;
}
}
@end
B頁(yè)面代碼:
B頁(yè)面 .h 代碼
#import <UIKit/UIKit.h>
//委托方 - 創(chuàng)建一個(gè)協(xié)議
@protocol passValueDelegate <NSObject>
//協(xié)議定義一個(gè)傳值的方法
- (void)passValue:(NSString *)str;
@end
@interface D_2_ViewController : UIViewController
//聲明全局代理協(xié)議
@property (weak) id<passValueDelegate>delegate;
@end
B頁(yè)面 .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頁(yè)面" 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];
}
//返回頁(yè)面一
- (void)btnClick
{
//代理傳值 - 反向傳值
[self.delegate passValue:self.textF.text];
//退出模態(tài)頁(yè)面
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
5. Block傳值
傳值描述:
A頁(yè)面侵续、B頁(yè)面
A頁(yè)面-》B頁(yè)面-》B頁(yè)面聲明閉包(Block),并返回A頁(yè)面的同時(shí)使用閉包傳值(Block)-》A頁(yè)面獲取傳值
實(shí)現(xiàn)代碼步驟:
1.在B頁(yè)面中聲明全局的閉包(Block)憨闰,并設(shè)置好要參數(shù)類(lèi)型
2.在B頁(yè)面中使用閉包(Block)賦值給閉包的參數(shù)
3.在A頁(yè)面中在跳轉(zhuǎn)頁(yè)面之前状蜗,使用B頁(yè)面的閉包給A頁(yè)面的標(biāo)簽賦值
直接上代碼:
A頁(yè)面代碼:
#import "EViewController.h"
//導(dǎo)入B頁(yè)面頭文件
#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:@"跳轉(zhuǎn)B頁(yè)面" forState:UIControlStateNormal];
[_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//點(diǎn)擊事件
[_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
}
return _btn;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//把UI指針添加到View視圖找那個(gè)
[self.view addSubview:self.label];
[self.view addSubview:self.btn];
}
//button點(diǎn)擊事件 -- 跳轉(zhuǎn)到B頁(yè)面
- (void)btnClick
{
//聲明指針,并初始化
E_2_ViewController *e_2_VC = [[E_2_ViewController alloc]init];
//block傳值 - 實(shí)現(xiàn)block - 接收來(lái)自B頁(yè)面?zhèn)髦? e_2_VC.block = ^(NSString *str) {
self.label.text = str;
};
//模態(tài)進(jìn)入B頁(yè)面
[self presentViewController:e_2_VC animated:YES completion:nil];
}
@end
B頁(yè)面代碼:
B頁(yè)面 .h 代碼:
#import <UIKit/UIKit.h>
@interface E_2_ViewController : UIViewController
//定義一個(gè)block進(jìn)行頁(yè)面反向傳值;在創(chuàng)建的時(shí)候鹉动,要使用copy轧坎,這是為了循環(huán)使用
@property (copy) void (^block)(NSString *);
@end
B頁(yè)面 .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頁(yè)面" 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頁(yè)面
- (void)btnClick
{
//block傳值 - 反向傳值
self.block(self.textF.text);
//退出模態(tài)頁(yè)面
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
2. 通知傳值
傳值描述:
A頁(yè)面、B頁(yè)面
A頁(yè)面-》B頁(yè)面-》B頁(yè)面使用通知(NotificationCenter)傳值-》A頁(yè)面使用通知(NotificationCenter)獲取傳值
實(shí)現(xiàn)代碼步驟:
1.在B頁(yè)面中使用通知中心(NotificationCenter)調(diào)用post去賦值泽示,同時(shí)發(fā)送通知消息
2.在B頁(yè)面中需要實(shí)現(xiàn)“移除通知”缸血,必須實(shí)現(xiàn)這個(gè)代碼
3.在A頁(yè)面中使用通知中心(NotificationCenter)調(diào)用addObserver來(lái)設(shè)置通知的監(jiān)聽(tīng)事件
4.實(shí)現(xiàn)通知中心(NotificationCenter)的監(jiān)聽(tīng)事件,并在方法里獲取B頁(yè)面?zhèn)鬟^(guò)來(lái)的值
注:注冊(cè)通知中心的“Name”值械筛,在A捎泻、B頁(yè)面中通知中心的“Name”值必須一致,否則獲取不到傳值
直接上代碼:
A頁(yè)面代碼:
#import "FViewController.h"
//導(dǎo)入B頁(yè)面頭文件
#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:@"跳轉(zhuǎn)B頁(yè)面" forState:UIControlStateNormal];
[_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//點(diǎn)擊事件
[_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點(diǎn)擊事件 -- 跳轉(zhuǎn)到頁(yè)面二
- (void)btnClick
{
//聲明指針埋哟,并初始化
F_2_ViewController *f_2_VC = [[F_2_ViewController alloc]init];
//通知傳值 - 添加監(jiān)聽(tīng)等待頁(yè)面二的傳值
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notHandle:) name:@"notify" object:nil];
//跳轉(zhuǎn)B頁(yè)面
[self.navigationController pushViewController:f_2_VC animated:YES];
// [self presentViewController:f_2_VC animated:YES completion:nil];
}
//接收到通知之后的處理 - 參數(shù)1:通知
- (void)notHandle:(NSNotification *)not
{
//把獲取的傳值族扰,賦值給標(biāo)簽
self.label.text = not.userInfo[@"not"];
}
@end
B頁(yè)面代碼:
#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頁(yè)面" 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頁(yè)面
- (void)btnClick
{
//通知傳值 - 發(fā)送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"notify" object:nil userInfo:@{@"not":self.textF.text}];
//返回A頁(yè)面
[self.navigationController popViewControllerAnimated:YES];
}
@end
傳值的幾種方式,根據(jù)自己的需求來(lái)使用定欧,其中渔呵,有幾個(gè)可以實(shí)現(xiàn)正向傳值、反向傳值砍鸠,這里就不再寫(xiě)了扩氢,太長(zhǎng)了,也太累爷辱,原諒我的懶惰吧录豺!
代碼請(qǐng)到gitee上下載:https://gitee.com/h8900961/ios-value-transfer-mode
有任何問(wèn)題朦肘,請(qǐng)留言,我會(huì)第一時(shí)間去處理双饥。
謝謝媒抠,瀏覽!