IOS VC间传值的几种方法

字数 150阅读 862

看到一个demo文件, 将IOS VC间传值的方法进行罗列整理,供学习参考。

1.初始化传值

  • VC1
/***********初始化传值**************/

ZQMainVC *mainVC=[[ZQMainVC alloc] initWithUserName:_userNameText.text WithPassWord:_passWordText.text];

[self.navigationController pushViewController:mainVC animated:YES];

/***********初始化传值****************/

  • VC2
-(id)initWithUserName:(NSString *)userName WithPassWord:(NSString *)passWord {
    self = [super init];
    if (self) {
            // Custom initialization
            _passWord=passWord;
            _userName=userName;
     }
            return self;
}

2.代理传值

  • VC1
/***********代理传值*******************/
        
        ZQMainVC *mainVC=[[ZQMainVC alloc]init];
        self.delegate = mainVC;
        
        if (self.delegate&&[self.delegate respondsToSelector:@selector(ViewControllerSendMessage:withPassword:)]) {
            [self.delegate ViewControllerSendMessage:_userNameText.text withPassword:_passWordText.text];
        }
        
        [self.navigationController pushViewController:mainVC animated:YES];
       
    /***********代理传值*******************/
  • VC2 接收代理方法
#pragma mark ViewControllerDelegate
-(void)ViewControllerSendMessage:(NSString *)userName withPassword:(NSString *)password{
    
    _userName=userName;
    _passWord=password;
}

3.单例传值

  • VC1
// 申明初始化单例类的对象
    ZQShareManager *shareManager = [ZQShareManager shareUserInfo];
    _userNameText.text = shareManager.userName;
    _passWordText.text = shareManager.password;

  • 单例方法 .h
#import <Foundation/Foundation.h>

@interface ZQShareManager : NSObject

@property (copy, nonatomic) NSString *userName;
@property (copy, nonatomic) NSString *password;
// 申明一个类方法
+ (ZQShareManager *)shareUserInfo;

@end

  • 单例方法 .m
#import "ZQShareManager.h"

@implementation ZQShareManager
+ (ZQShareManager *)shareUserInfo{
    static ZQShareManager *shareManager = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        shareManager = [[self alloc] init];
    });
    return shareManager;
}
@end
  • VC2
// 正向传值,直接从单例类里面读取数据
    ZQShareManager *shareManager = [ZQShareManager shareUserInfo];   
    _userName = shareManager.userName;
    _passWord = shareManager.password;

4.属性传值

  • VC1

需要import 头文件

  /***********属性传值*******************/
        ZQMainVC *mainVC=[[ZQMainVC alloc]init];
        mainVC.userName=_userNameText.text;
        mainVC.passWord=_passWordText.text;
        [self.navigationController pushViewController:mainVC animated:YES];
    /***********属性传值*******************/

  • VC2

.h 文件声明属性

#import <UIKit/UIKit.h>
@interface ZQMainVC : UIViewController
@property(nonatomic,strong)NSString *userName;
@property(nonatomic,strong)NSString *passWord;

5.通知传值

  • VC1

发送通知

/***************通知传值****************/
    NSArray *array=[[NSArray alloc]initWithObjects:_userNameText.text,_passWordText.text, nil];
    //发送通知  sendMessage表示通知详情 array表示传输数据
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ZQSendMessage" object:array];
    /*****************通知传值****************/
    [self.navigationController popToRootViewControllerAnimated: YES];
  • VC2

接收通知

/*****通知*********/
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getZQMessage:) name:@"ZQSendMessage" object:nil];
 /*****通知************/

-(void)getZQMessage:(NSNotification *)notification{
    
    NSArray *array=[[NSArray alloc]initWithArray:notification.object];
    _userNameText.text=array[0];
    _passWordText.text=array[1];
    
}

6.block传值

  • VC1 (两种方法)
       ZQResignVC *resignVC=[[ZQResignVC alloc]init];
        //直接定义block
        resignVC.block=^(NSArray *array){
            _userNameText.text=array[0];
            _passWordText.text=array[1];
        };
        //通过方法
       [resignVC sendMessage:^(NSArray *array) {
           _userNameText.text=array[0];
           _passWordText.text=array[1];
       }];
        [self.navigationController pushViewController:resignVC animated:YES];

-(void)sendMessage:(myBlcok)block{
        self.block=block;
}
  • VC2 (.h文件)
#import <UIKit/UIKit.h>

// block格式: 返回值(^block名字)(参数)
// (1)定义block
typedef void(^myBlcok)(NSArray *array);

@interface ZQResignVC : UIViewController
// (2)申明block属性
@property (strong, nonatomic) myBlcok block;

-(void)sendMessage:(myBlcok)block;

@end

7.AppDelegate传值 (全局方法 不推荐)

需要导入#import "AppDelegate.h"

  • AppDelegate.h文件
// 申明数据源数组保存所需要的数据
@property (strong, nonatomic) NSArray *dataArray;
  • VC1
 // 获取AppDelegate对象
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    
    if (appDelegate.dataArray == nil)
    {
        appDelegate.dataArray = [[NSArray alloc] init];
    }
    
    appDelegate.dataArray = [NSArray arrayWithObjects:_userNameText.text,_passWordText.text, nil];
       [self.navigationController popToRootViewControllerAnimated: YES];
  • VC2
- (void)viewDidAppear:(BOOL)animated
{
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    
    _userNameText.text = (NSString *)appDelegate.dataArray[0];
    _passWordText.text = (NSString *)appDelegate.dataArray[1];
    
    [super viewDidAppear:animated];
}

8.NSUserDefault传值 (系统自带)

  • VC1
/***********NSUserDefaults传值****************/
        [[NSUserDefaults standardUserDefaults]setValue:_userNameText.text forKey:@"UserId"];
        [[NSUserDefaults standardUserDefaults]setValue:_passWordText.text forKey:@"PassWord"];
        //将缓存中的数据强制写入磁盘
        [[NSUserDefaults standardUserDefaults]synchronize];
        ZQMainVC *mainVC=[[ZQMainVC alloc]init];
         [self.navigationController pushViewController:mainVC animated:YES];
  /***********NSUserDefaults传值****************/
  • VC2
 /**************NSUserDefaults传值**************/
    self.userName=[[NSUserDefaults standardUserDefaults]objectForKey:@"UserId"];
    self.passWord=[[NSUserDefaults standardUserDefaults]objectForKey:@"PassWord"];
    /************NSUserDefaults传值**********/

推荐阅读更多精彩内容