iOS - 頁(yè)面之間幾個(gè)常用的傳值方式

頁(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í)間去處理双饥。
謝謝媒抠,瀏覽!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末咏花,一起剝皮案震驚了整個(gè)濱河市趴生,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌昏翰,老刑警劉巖苍匆,帶你破解...
    沈念sama閱讀 217,826評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異棚菊,居然都是意外死亡浸踩,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)统求,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)检碗,“玉大人,你說(shuō)我怎么就攤上這事码邻『舐悖” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,234評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵冒滩,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我浪谴,道長(zhǎng)开睡,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,562評(píng)論 1 293
  • 正文 為了忘掉前任苟耻,我火速辦了婚禮篇恒,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘凶杖。我一直安慰自己胁艰,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布智蝠。 她就那樣靜靜地躺著腾么,像睡著了一般。 火紅的嫁衣襯著肌膚如雪杈湾。 梳的紋絲不亂的頭發(fā)上解虱,一...
    開(kāi)封第一講書(shū)人閱讀 51,482評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音漆撞,去河邊找鬼殴泰。 笑死于宙,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的悍汛。 我是一名探鬼主播捞魁,決...
    沈念sama閱讀 40,271評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼离咐!你這毒婦竟也來(lái)了谱俭?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,166評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤健霹,失蹤者是張志新(化名)和其女友劉穎旺上,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體糖埋,經(jīng)...
    沈念sama閱讀 45,608評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡宣吱,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了瞳别。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片征候。...
    茶點(diǎn)故事閱讀 39,926評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖祟敛,靈堂內(nèi)的尸體忽然破棺而出疤坝,到底是詐尸還是另有隱情,我是刑警寧澤馆铁,帶...
    沈念sama閱讀 35,644評(píng)論 5 346
  • 正文 年R本政府宣布跑揉,位于F島的核電站,受9級(jí)特大地震影響埠巨,放射性物質(zhì)發(fā)生泄漏历谍。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評(píng)論 3 329
  • 文/蒙蒙 一辣垒、第九天 我趴在偏房一處隱蔽的房頂上張望望侈。 院中可真熱鬧,春花似錦勋桶、人聲如沸脱衙。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,866評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)捐韩。三九已至,卻和暖如春鹃锈,著一層夾襖步出監(jiān)牢的瞬間奥帘,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,991評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工仪召, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留寨蹋,地道東北人松蒜。 一個(gè)月前我還...
    沈念sama閱讀 48,063評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像已旧,于是被迫代替她去往敵國(guó)和親秸苗。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容