iOS 页面传值知多少?你真的了解吗?

iOS开发中,页面传值是很常见的,但是页面传值你究竟知道多少呢?笔者这篇文章就是给大家介绍一下页面传值的具体方式,有不足之处,欢迎大家指正,希望能和大家共同进步。说明一下:这里所说的正向、反向传值是指相关联的两个页面间的传值。

目前我所了解和掌握的传值方式有:属性传值、代理传值、Block传值、KVO传值、通知传值、单例传值、KVC传值。

下面我们来一一看下它们究竟是怎样进行操作和传值的呢?

假设我们现在有控制器(页面)A和控制器(页面)B,A->push->B,即A是B的上一个页面(控制器)。

  1. 属性传值
    用法:正向传值
    需求:当A-push到B时,B中有一个Label需要显示从A中的一个TextField输入的内容,这时我们需要用到正向传值。

    A控制器.m文件:

     #import "A_ViewController.h"
     #import "B_ViewController.h"
     
     @interface A_ViewController ()
     
     @property (nonatomic, strong) UITextField *aTextField;
     
     @end
     
     @implementation A_ViewController
     
     - (void)viewDidLoad {
         [super viewDidLoad];
         
         self.title = @"A";
         self.view.backgroundColor = [UIColor whiteColor];
         self.aTextField = [[UITextField alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
        
         self.aTextField.layer.borderColor = [UIColor grayColor].CGColor;
         self.aTextField.layer.borderWidth = 1;
         [self.view addSubview:self.aTextField];
         
         UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
         button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
         [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
         [button setTitle:@"传到B" forState:UIControlStateNormal];
         [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
         [self.view addSubview:button];
         
     }
     
     - (void)buttonAction:(UIButton *)sender {
     
         /**
          什么时候可以用 属性 传值
          
          A 传到 B,正向传值
          
          B 在 A页面 提前初始化
          
          **/
         
         B_ViewController *bViewController = [[B_ViewController alloc] init];
         bViewController.string = self.aTextField.text;
         [self.navigationController pushViewController:bViewController animated:YES];
         
     }
    
     @end
    

    B控制器.h文件:

     #import <UIKit/UIKit.h>
     
     @interface B_ViewController : UIViewController
     
     @property (nonatomic, copy) NSString *string;
     
     @end
    

    B控制器.m文件:

     #import "B_ViewController.h"
    
     @interface B_ViewController ()
     
     @end
     
     @implementation B_ViewController
     
     - (void)viewDidLoad {
         [super viewDidLoad];
         
         self.title = @"B";
         self.view.backgroundColor = [UIColor whiteColor];
         
         self.view.backgroundColor = [UIColor whiteColor];
         UILabel *bLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
         
         bLabel.layer.borderColor = [UIColor grayColor].CGColor;
         bLabel.layer.borderWidth = 1;
         [self.view addSubview:bLabel];
         
         bLabel.text = self.string;
     }
    
     @end
    
  2. 代理传值
    用法:反向传值:
    需求:A-push到B,当B消失的时候,A中有一个Label需要显示从B中的一个TextField输入的内容,这时我们需要用到反向传值。

    A控制器.m文件:

     #import "A_ViewController.h"
     #import "B_ViewController.h"
     
     @interface A_ViewController () <BToADelegate>
     
     @property (nonatomic, strong) UILabel *aLabel;
     
     @end
     
     @implementation A_ViewController
     
     - (void)viewDidLoad {
         [super viewDidLoad];
         
         self.title = @"A";
         self.view.backgroundColor = [UIColor whiteColor];
         self.aLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
         
         self.aLabel.layer.borderColor = [UIColor grayColor].CGColor;
         self.aLabel.layer.borderWidth = 1;
         [self.view addSubview:self.aLabel];
         
         UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
         button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
         [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
         [button setTitle:@"push到B" forState:UIControlStateNormal];
         [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
         [self.view addSubview:button];
         
     }
     
     -(void)buttonAction:(UIButton *)sender {
        
         B_ViewController *bViewController = [[B_ViewController alloc] init];
         //设置代理
         bViewController.delegate = self;
         [self.navigationController pushViewController:bViewController animated:YES];
     }
     
     /**
      什么时候可以用 代理 传值
      
      B 传到 A,反向传值
      
      B 在 A页面 初始化
      
      设置A为B的代理
      
      执行代理方法
      
      **/
     - (void)transferString:(NSString *)string {
         
         self.aLabel.text = string;
     }
     
     @end
    

    B控制器.h文件:

     #import <UIKit/UIKit.h>
     
     // 声明代理
     @protocol BToADelegate <NSObject>
     
     // 代理方法
     - (void)transferString:(NSString *)string;
     
     @end
     
     @interface B_ViewController : UIViewController
     
     // 代理属性
     @property (nonatomic, weak) id<BToADelegate> delegate;
     
     @end
    

    B控制器.m文件:

     #import "B_ViewController.h"
     
     @interface B_ViewController ()
     
     @property (nonatomic, strong) UITextField *bTextField;;
     
     @end
     
     @implementation B_ViewController
     
     - (void)viewDidLoad {
         [super viewDidLoad];
         
         self.title = @"B";
         self.view.backgroundColor = [UIColor whiteColor];
         
         self.view.backgroundColor = [UIColor whiteColor];
         self.bTextField = [[UITextField alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
         
         self.bTextField.layer.borderColor = [UIColor grayColor].CGColor;
         self.bTextField.layer.borderWidth = 1;
         [self.view addSubview:self.bTextField];
         
         UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
         button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
         [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
         [button setTitle:@"传值A" forState:UIControlStateNormal];
         [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
         [self.view addSubview:button];
         
     }
     
     - (void)buttonAction:(UIButton *)sender {
         
         // 判断有没有代理以及代理是否响应代理方法
         if (self.delegate && [self.delegate respondsToSelector:@selector(transferString:)]) {
             [self.delegate transferString:self.bTextField.text];
         }
         
         [self.navigationController popToRootViewControllerAnimated:YES];
     }
     
     @end
    
  3. Block传值
    用法:反向传值:
    需求:A-push到B,当B消失的时候,A中有一个Label需要显示从B中的一个TextField输入的内容,这时我们需要用到反向传值。

    A控制器.m文件:

     #import "A_ViewController.h"
     #import "B_ViewController.h"
     
     @interface A_ViewController ()
     
     @property (nonatomic ,strong) UILabel *aLabel;
     
     @end
     
     @implementation A_ViewController
     
     - (void)viewDidLoad {
         [super viewDidLoad];
         
         self.title = @"A";
         self.view.backgroundColor = [UIColor whiteColor];
         self.aLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
         
         self.aLabel.layer.borderColor = [UIColor grayColor].CGColor;
         self.aLabel.layer.borderWidth = 1;
         [self.view addSubview:self.aLabel];
         
         UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
         button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
         [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
         [button setTitle:@"push到B" forState:UIControlStateNormal];
         [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
         [self.view addSubview:button];
         
     }
     
     - (void)buttonAction:(UIButton *)sender {
     
         B_ViewController *bViewController = [[B_ViewController alloc] init];
     
         __weak __typeof(self) weakSelf = self;
         // block 回调接收
         [bViewController setBlock:^(NSString *string){
             weakSelf.aLabel.text = string;
         }];
         
         [self.navigationController pushViewController:bViewController animated:YES];
     }
    

    B控制器.h文件:

     #import <UIKit/UIKit.h>
     
     // 定义一个block
     typedef void(^BToAblock)(NSString *string);
     
     @interface B_ViewController : UIViewController
     
     // block 属性
     @property (nonatomic, copy)BToAblock block;
     
     @end
    

    B控制器.m文件:

     #import "B_ViewController.h"
     
     @interface B_ViewController ()
     
     @property (nonatomic, strong) UITextField *bTextField;
     
     @end
     
     @implementation B_ViewController
     
     - (void)viewDidLoad {
         [super viewDidLoad];
         
         self.title = @"B";
         self.view.backgroundColor = [UIColor whiteColor];
         
         self.view.backgroundColor = [UIColor whiteColor];
         self.bTextField = [[UITextField alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
         
         self.bTextField.layer.borderColor = [UIColor grayColor].CGColor;
         self.bTextField.layer.borderWidth = 1;
         [self.view addSubview:self.bTextField];
         
         UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
         button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
         [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
         [button setTitle:@"传值A" forState:UIControlStateNormal];
         [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
         [self.view addSubview:button];
     
     }
     
     - (void)buttonAction:(UIButton *)sender {
         /**
          Blcok 传值
             
          反向传值
          
          B 传到 A
          */
         _block(self.bTextField.text);
         [self.navigationController popToRootViewControllerAnimated:YES];
     }
     
     @end
    
  4. KVO传值
    用法:反向传值:
    需求:A-push到B,当B消失的时候,A中有一个Label需要显示从B中的一个TextField输入的内容,这时我们需要用到反向传值。

    A控制器.m文件:

     #import "A_ViewController.h"
     #import "B_ViewController.h"
     
     @interface A_ViewController ()
     
     @property (nonatomic, strong) UILabel *aLabel;
     @property (nonatomic, strong) B_ViewController *bViewController;
     
     @end
     
     @implementation A_ViewController
     
     - (void)viewDidLoad {
         [super viewDidLoad];
         
         self.title = @"A";
         self.view.backgroundColor = [UIColor whiteColor];
         self.aLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
         
         self.aLabel.layer.borderColor = [UIColor grayColor].CGColor;
         self.aLabel.layer.borderWidth = 1;
         [self.view addSubview:self.aLabel];
         
         UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
         button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
         [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
         [button setTitle:@"push到B" forState:UIControlStateNormal];
         [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
         [self.view addSubview:button];
         
         /**
          KVO 创建 三步一定要写
             1. 注册观察者
             2. KVO的回调
             3. 移除观察者
          
          */
         
         // B 传到 A ,反向传值
         //注册观察者,注意:观察者的注册和移除要对应,如果移除时发现没有注册观察者,程序会crash
          self.bViewController = [[B_ViewController alloc] init];
         [self.bViewController addObserver:self forKeyPath:@"string" options:NSKeyValueObservingOptionNew context:nil];
     }
     
     - (void)buttonAction:(UIButton *)sender {
         
         [self.navigationController pushViewController:self.bViewController animated:YES];
         
     }
     
     // KVO的回调
     - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
         
         if ([keyPath isEqualToString:@"string"]) {
             self.aLabel.text = self.bViewController.string;
         }
         
     }
     // KVO 的移除方式  (和通知一样要移除)
     - (void)dealloc {
         
         [self.bViewController removeObserver:self forKeyPath:@"string"];
     }
     
     @end
    

    B控制器.h文件:

     #import <UIKit/UIKit.h>
     
     @interface B_ViewController : UIViewController
     
     @property (nonatomic, copy) NSString *string;
     
     @end
    

    B控制器.m文件:

     #import "B_ViewController.h"
     
     @interface B_ViewController ()
     
     @property (nonatomic, strong) UITextField *bTextField;
     
     @end
     
     @implementation B_ViewController
     
     - (void)viewDidLoad {
         [super viewDidLoad];
         
         self.title = @"B";
         self.view.backgroundColor = [UIColor whiteColor];
         
         self.view.backgroundColor = [UIColor whiteColor];
         self.bTextField = [[UITextField alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
         
         self.bTextField.layer.borderColor = [UIColor grayColor].CGColor;
         self.bTextField.layer.borderWidth = 1;
         [self.view addSubview:self.bTextField];
         
         UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
         button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
         [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
         [button setTitle:@"传值A" forState:UIControlStateNormal];
         [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
         [self.view addSubview:button];
         
     }
     
     - (void)buttonAction:(UIButton *)sender {
         // KVO
         // 把self.bTextfield.text 赋值给当前属性
         // 在A中 监听 当前属性
         self.string = self.bTextField.text;
         
         [self.navigationController popToRootViewControllerAnimated:YES];
     }
     
     @end
    
  5. 通知传值
    用法:正向传值
    需求:当A-push到B时,B中有一个Label需要显示从A中的一个TextField输入的内容,这时我们需要用到正向传值。

    用法:反向传值:
    需求:A-push到B,当B消失的时候,A中有一个Label需要显示从B中的一个TextField输入的内容,这时我们需要用到反向传值。

    我们在此将两种传值情况写到一个Demo中,所以将上述Label换为Textfield即可,如下:

    A控制器.m文件:

     #import "A_ViewController.h"
     #import "B_ViewController.h"
     
     @interface A_ViewController ()
     
     @property (nonatomic, strong) UITextField *aTextField;
     
     @end
     
     @implementation A_ViewController
     
     - (void)viewDidLoad {
         [super viewDidLoad];
         
         self.title = @"A";
         self.view.backgroundColor = [UIColor whiteColor];
         self.aTextField = [[UITextField alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
    
         self.aTextField.layer.borderColor = [UIColor grayColor].CGColor;
         self.aTextField.layer.borderWidth = 1;
         [self.view addSubview:self.aTextField];
         
         UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
         button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
         [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
         [button setTitle:@"传到B" forState:UIControlStateNormal];
         [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
         [self.view addSubview:button];
         
         // 接收通知
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tzAction:) name:@"B2A" object:nil];
     }
     
     - (void)buttonAction:(UIButton *)sender {
     
         // 通知传值 一般是用于回传
         
         // 现在是 A传到B,正向传值
         
         // 发送通知的方法 要写在执行方法里面
         
         B_ViewController *bViewController = [[B_ViewController alloc] init];
         
         [[NSNotificationCenter defaultCenter] postNotificationName:@"A2B" object:nil userInfo:@{@"key":self.aTextField.text}];
         
         [self.navigationController pushViewController:bViewController animated:YES];
         
     }
     
     // 回调通知
     - (void)tzAction:(NSNotification *)sender {
         
         self.aTextField.text = sender.userInfo[@"key"];
     }
     
     // 移除通知
     - (void)dealloc {
         
         [[NSNotificationCenter defaultCenter] removeObserver:self];
     }
     
     @end
    

    B控制器.m文件:

     #import "B_ViewController.h"
     
     @interface B_ViewController ()
     
     @property (nonatomic, strong) UITextField *bTextField;
     
     @end
     
     @implementation B_ViewController
     
     - (void)viewDidLoad {
         [super viewDidLoad];
         
         self.title = @"B";
         self.view.backgroundColor = [UIColor whiteColor];
         
         UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
         button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
         [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
         [button setTitle:@"传值A" forState:UIControlStateNormal];
         [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
         [self.view addSubview:button];
     }
     
     //如果是从A传到B的话,B.m里要创建一个init方法,在里面写监听并在里面创建接收容器才能成功(因为程序先执行init方法再到viewDidLoad方法,当传值过去时在init就开始监听,如果这里没有创建textField接收,那就传不过去了,所以要在init里同时创建接收器(生命周期的问题));
     -(instancetype)init
     {
         if (self = [super init]) {
    
             [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tzAction:) name:@"A2B" object:nil];
             
             self.bTextField = [[UITextField alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
            
             self.bTextField.layer.borderColor = [UIColor grayColor].CGColor;
             self.bTextField.layer.borderWidth = 1;
             [self.view addSubview:self.bTextField];
     
         }
     
         return self;
     }
     
     //接收通知
     - (void)tzAction:(NSNotification *)sender {
         
         self.bTextField.text = sender.userInfo[@"key"];
         
     }
     
     // 移除通知
     - (void)dealloc  {
         
         // 移除所有
         [[NSNotificationCenter defaultCenter] removeObserver:self];
         
         // 移除某个
         // [[NSNotificationCenter defaultCenter] removeObserver:self name:@"tz" object:nil];
     }
     
     //发送通知
     - (void)buttonAction:(UIButton *)sender {
         // B传到A,反向传值
         [[NSNotificationCenter defaultCenter] postNotificationName:@"B2A" object:nil userInfo:@{@"key":self.bTextField.text}];
         
         [self.navigationController popToRootViewControllerAnimated:YES];
         
     }
     
     @end
    
  6. 单例传值
    用法:正向传值
    需求:当A-push到B时,B中有一个Label需要显示从A中的一个TextField输入的内容,这时我们需要用到正向传值。

    用法:反向传值:
    需求:A-push到B,当B消失的时候,A中有一个Label需要显示从B中的一个TextField输入的内容,这时我们需要用到反向传值。

    我们在此将两种传值情况写到一个Demo中,所以将上述Label换为Textfield即可,如下:

    A控制器.m文件:

     #import "A_ViewController.h"
     #import "B_ViewController.h"
     #import "DanLi.h"
     
     @interface A_ViewController ()
     
     @property (nonatomic, strong) UITextField *aTextField;
     
     @end
     
     @implementation A_ViewController
     
     - (void)viewDidLoad {
         [super viewDidLoad];
         // Do any additional setup after loading the view.
         
         self.title = @"A";
         self.view.backgroundColor = [UIColor whiteColor];
         self.aTextField = [[UITextField alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
         
         self.aTextField.layer.borderColor = [UIColor grayColor].CGColor;
         self.aTextField.layer.borderWidth = 1;
         [self.view addSubview:self.aTextField];
         
         UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
         button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
         [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
         [button setTitle:@"传到B" forState:UIControlStateNormal];
         [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
         [self.view addSubview:button];
         
     }
     
     - (void)viewWillAppear:(BOOL)animated
     {
         // 单例 传值 B传到A (可以双向传值)
         DanLi *danli = [[DanLi alloc] init];
         
         self.aTextField.text = danli.value;
     }
     
     - (void)buttonAction:(UIButton *)sender {
         
         // 单例 传值 A传到B (可以双向传值)
         DanLi *danli = [DanLi sharedDanLi];
         danli.value = self.aTextField.text;
      
         B_ViewController *bViewController = [[B_ViewController alloc] init];
     
         [self.navigationController pushViewController:bViewController animated:YES];
         
     }
    

    B控制器.m文件:

     #import "B_ViewController.h"
     #import "DanLi.h"
     
     @interface B_ViewController ()
     
     @property (nonatomic, strong) UITextField *bTextField;
     
     @end
     
     @implementation B_ViewController
     
     - (void)viewDidLoad {
         [super viewDidLoad];
         
         self.title = @"B";
         self.view.backgroundColor = [UIColor whiteColor];
         
         self.view.backgroundColor = [UIColor whiteColor];
         self.bTextField = [[UITextField alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
         
         self.bTextField.layer.borderColor = [UIColor grayColor].CGColor;
         self.bTextField.layer.borderWidth = 1;
         [self.view addSubview:self.bTextField];
         
         UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
         button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
         [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
         [button setTitle:@"传值A" forState:UIControlStateNormal];
         [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
         [self.view addSubview:button];
         
         self.bTextField.text = [DanLi sharedDanLi].value;
     }
     
     - (void)buttonAction:(UIButton *)sender {
         //B传给A
         [DanLi sharedDanLi].value = self.bTextField.text;
         [self.navigationController popToRootViewControllerAnimated:YES];
     }
     
     @end
    

    单例类.h文件:

     #import <Foundation/Foundation.h>
     
     @interface DanLi : NSObject
     
     //创建一个单例//如果在单线程里可以用nonatomic,如果在多线程里一定要用atomic,保证是只有一个在调用,不然在多线程里面如果多个方法调用修改单例类里的属性时会冲突
     @property (atomic, copy) NSString *value;
     
     + (DanLi *)sharedDanLi;
     
     @end
    

    单例类.m文件:

     #import "DanLi.h"
     
     static DanLi *danli = nil;
     
     @implementation DanLi
     
     //实现方法,判断是否为空,是就创建一个全局实例给它
     + (DanLi *)sharedDanLi {
         
         if (danli == nil) {
             danli = [[DanLi alloc] init];
         }
         return danli;
     }
     
     //避免alloc/new创建新的实例变量--->增加一个互斥锁
     + (id)allocWithZone:(struct _NSZone *)zone {
         
         @synchronized(self) {
             if (danli == nil) {
                 danli = [super allocWithZone:zone];
             }
         }
         return danli;
     }
     
     //避免copy,需要实现NSCopying协议
     - (id)copyWithZone:(NSZone *)zone {
         return self;
     }
     
     @end
    
  7. KVC传值
    用法:正向传值
    需求:当A-push到B时,B中有一个Label需要显示从A中的一个TextField输入的内容,这时我们需要用到正向传值。

    A控制器.m文件:

     #import "A_ViewController.h"
     #import "B_ViewController.h"
     
     @interface A_ViewController ()
     
     @property (nonatomic, strong) UITextField *aTextField;
     
     @end
     
     @implementation A_ViewController
     
     - (void)viewDidLoad {
         [super viewDidLoad];
         // Do any additional setup after loading the view.
         self.title = @"A";
         self.view.backgroundColor = [UIColor whiteColor];
         self.aTextField = [[UITextField alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
         
         self.aTextField.layer.borderColor = [UIColor grayColor].CGColor;
         self.aTextField.layer.borderWidth = 1;
         [self.view addSubview:self.aTextField];
         
         UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
         button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
         [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
         [button setTitle:@"传到B" forState:UIControlStateNormal];
         [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
         [self.view addSubview:button];
     }
    
     - (void)buttonAction:(UIButton *)sender {
     
         B_ViewController *bViewController = [[B_ViewController alloc] init];
     
         /**
             KVC 传值:这里只能传A传到B (因为 B在A页面提前初始化)
             B 有个属性 string
             用B对象 给B属性赋值(回顾下OC中KVC赋值 就理解了)
          
             这里forkey 一定要和B 属性名字一致 (也可以用@"_string")因为是属性
          */
         
         // 给B属性string  赋值
         [bViewController setValue:self.aTextField.text forKey:@"string"];
         
         [self.navigationController pushViewController:bViewController animated:YES];
     }
     
     @end
    

    B控制器.h文件:

     #import <UIKit/UIKit.h>
     
     @interface B_ViewController : UIViewController
     
     @property (nonatomic, copy) NSString *string;
     
     @end
    

    B控制器.m文件:

     #import "B_ViewController.h"
     
     @interface B_ViewController ()
     
     @end
     
     @implementation B_ViewController
     
     - (void)viewDidLoad {
         [super viewDidLoad];
         
         self.title = @"B";
         self.view.backgroundColor = [UIColor whiteColor];
         
         self.view.backgroundColor = [UIColor whiteColor];
         UILabel *bLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
         
         bLabel.layer.borderColor = [UIColor grayColor].CGColor;
         bLabel.layer.borderWidth = 1;
         [self.view addSubview:bLabel];
         
         UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
         button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
         [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
         [button setTitle:@"传值A" forState:UIControlStateNormal];
         [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
         [self.view addSubview:button];
         
         // KVC 接收值
         bLabel.text = self.string;
     }
     
     - (void)buttonAction:(UIButton *)sender {
         
         [self.navigationController popToRootViewControllerAnimated:YES];
     }
     
     @end
    

好了,到此已经基本上介绍完页面传值了,相信你对页面传值已经有一定理解了吧,快去实践吧!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,324评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,303评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,192评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,555评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,569评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,566评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,927评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,583评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,827评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,590评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,669评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,365评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,941评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,928评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,159评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,880评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,399评论 2 342

推荐阅读更多精彩内容