iOS 頁面?zhèn)髦抵嗌伲磕阏娴牧私鈫幔?/h1>

iOS開發(fā)中,頁面?zhèn)髦凳呛艹R姷模琼撁鎮(zhèn)髦的憔烤怪蓝嗌倌兀抗P者這篇文章就是給大家介紹一下頁面?zhèn)髦档木唧w方式,有不足之處,歡迎大家指正,希望能和大家共同進(jìn)步。說明一下:這里所說的正向、反向傳值是指相關(guān)聯(lián)的兩個(gè)頁面間的傳值。

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

下面我們來一一看下它們究竟是怎樣進(jìn)行操作和傳值的呢?

假設(shè)我們現(xiàn)在有控制器(頁面)A和控制器(頁面)B,A->push->B,即A是B的上一個(gè)頁面(控制器)。

  1. 屬性傳值
    用法:正向傳值
    需求:當(dāng)A-push到B時(shí),B中有一個(gè)Label需要顯示從A中的一個(gè)TextField輸入的內(nèi)容,這時(shí)我們需要用到正向傳值。

    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 {
     
         /**
          什么時(shí)候可以用 屬性 傳值
          
          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,當(dāng)B消失的時(shí)候,A中有一個(gè)Label需要顯示從B中的一個(gè)TextField輸入的內(nèi)容,這時(shí)我們需要用到反向傳值。

    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];
         //設(shè)置代理
         bViewController.delegate = self;
         [self.navigationController pushViewController:bViewController animated:YES];
     }
     
     /**
      什么時(shí)候可以用 代理 傳值
      
      B 傳到 A,反向傳值
      
      B 在 A頁面 初始化
      
      設(shè)置A為B的代理
      
      執(zhí)行代理方法
      
      **/
     - (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(chǔ)" forState:UIControlStateNormal];
         [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
         [self.view addSubview:button];
         
     }
     
     - (void)buttonAction:(UIButton *)sender {
         
         // 判斷有沒有代理以及代理是否響應(yīng)代理方法
         if (self.delegate && [self.delegate respondsToSelector:@selector(transferString:)]) {
             [self.delegate transferString:self.bTextField.text];
         }
         
         [self.navigationController popToRootViewControllerAnimated:YES];
     }
     
     @end
    
  3. Block傳值
    用法:反向傳值:
    需求:A-push到B,當(dāng)B消失的時(shí)候,A中有一個(gè)Label需要顯示從B中的一個(gè)TextField輸入的內(nèi)容,這時(shí)我們需要用到反向傳值。

    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 回調(diào)接收
         [bViewController setBlock:^(NSString *string){
             weakSelf.aLabel.text = string;
         }];
         
         [self.navigationController pushViewController:bViewController animated:YES];
     }
    

    B控制器.h文件:

     #import <UIKit/UIKit.h>
     
     // 定義一個(gè)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(chǔ)" 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,當(dāng)B消失的時(shí)候,A中有一個(gè)Label需要顯示從B中的一個(gè)TextField輸入的內(nèi)容,這時(shí)我們需要用到反向傳值。

    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 創(chuàng)建 三步一定要寫
             1. 注冊(cè)觀察者
             2. KVO的回調(diào)
             3. 移除觀察者
          
          */
         
         // B 傳到 A ,反向傳值
         //注冊(cè)觀察者,注意:觀察者的注冊(cè)和移除要對(duì)應(yīng),如果移除時(shí)發(fā)現(xiàn)沒有注冊(cè)觀察者,程序會(huì)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的回調(diào)
     - (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(chǔ)" forState:UIControlStateNormal];
         [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
         [self.view addSubview:button];
         
     }
     
     - (void)buttonAction:(UIButton *)sender {
         // KVO
         // 把self.bTextfield.text 賦值給當(dāng)前屬性
         // 在A中 監(jiān)聽 當(dāng)前屬性
         self.string = self.bTextField.text;
         
         [self.navigationController popToRootViewControllerAnimated:YES];
     }
     
     @end
    
  5. 通知傳值
    用法:正向傳值
    需求:當(dāng)A-push到B時(shí),B中有一個(gè)Label需要顯示從A中的一個(gè)TextField輸入的內(nèi)容,這時(shí)我們需要用到正向傳值。

    用法:反向傳值:
    需求:A-push到B,當(dāng)B消失的時(shí)候,A中有一個(gè)Label需要顯示從B中的一個(gè)TextField輸入的內(nèi)容,這時(shí)我們需要用到反向傳值。

    我們?cè)诖藢煞N傳值情況寫到一個(gè)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 {
     
         // 通知傳值 一般是用于回傳
         
         // 現(xiàn)在是 A傳到B,正向傳值
         
         // 發(fā)送通知的方法 要寫在執(zhí)行方法里面
         
         B_ViewController *bViewController = [[B_ViewController alloc] init];
         
         [[NSNotificationCenter defaultCenter] postNotificationName:@"A2B" object:nil userInfo:@{@"key":self.aTextField.text}];
         
         [self.navigationController pushViewController:bViewController animated:YES];
         
     }
     
     // 回調(diào)通知
     - (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(chǔ)" forState:UIControlStateNormal];
         [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
         [self.view addSubview:button];
     }
     
     //如果是從A傳到B的話,B.m里要?jiǎng)?chuàng)建一個(gè)init方法,在里面寫監(jiān)聽并在里面創(chuàng)建接收容器才能成功(因?yàn)槌绦蛳葓?zhí)行init方法再到viewDidLoad方法,當(dāng)傳值過去時(shí)在init就開始監(jiān)聽,如果這里沒有創(chuàng)建textField接收,那就傳不過去了,所以要在init里同時(shí)創(chuàng)建接收器(生命周期的問題));
     -(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];
         
         // 移除某個(gè)
         // [[NSNotificationCenter defaultCenter] removeObserver:self name:@"tz" object:nil];
     }
     
     //發(fā)送通知
     - (void)buttonAction:(UIButton *)sender {
         // B傳到A,反向傳值
         [[NSNotificationCenter defaultCenter] postNotificationName:@"B2A" object:nil userInfo:@{@"key":self.bTextField.text}];
         
         [self.navigationController popToRootViewControllerAnimated:YES];
         
     }
     
     @end
    
  6. 單例傳值
    用法:正向傳值
    需求:當(dāng)A-push到B時(shí),B中有一個(gè)Label需要顯示從A中的一個(gè)TextField輸入的內(nèi)容,這時(shí)我們需要用到正向傳值。

    用法:反向傳值:
    需求:A-push到B,當(dāng)B消失的時(shí)候,A中有一個(gè)Label需要顯示從B中的一個(gè)TextField輸入的內(nèi)容,這時(shí)我們需要用到反向傳值。

    我們?cè)诖藢煞N傳值情況寫到一個(gè)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(chǔ)" 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
     
     //創(chuàng)建一個(gè)單例//如果在單線程里可以用nonatomic,如果在多線程里一定要用atomic,保證是只有一個(gè)在調(diào)用,不然在多線程里面如果多個(gè)方法調(diào)用修改單例類里的屬性時(shí)會(huì)沖突
     @property (atomic, copy) NSString *value;
     
     + (DanLi *)sharedDanLi;
     
     @end
    

    單例類.m文件:

     #import "DanLi.h"
     
     static DanLi *danli = nil;
     
     @implementation DanLi
     
     //實(shí)現(xiàn)方法,判斷是否為空,是就創(chuàng)建一個(gè)全局實(shí)例給它
     + (DanLi *)sharedDanLi {
         
         if (danli == nil) {
             danli = [[DanLi alloc] init];
         }
         return danli;
     }
     
     //避免alloc/new創(chuàng)建新的實(shí)例變量--->增加一個(gè)互斥鎖
     + (id)allocWithZone:(struct _NSZone *)zone {
         
         @synchronized(self) {
             if (danli == nil) {
                 danli = [super allocWithZone:zone];
             }
         }
         return danli;
     }
     
     //避免copy,需要實(shí)現(xiàn)NSCopying協(xié)議
     - (id)copyWithZone:(NSZone *)zone {
         return self;
     }
     
     @end
    
  7. KVC傳值
    用法:正向傳值
    需求:當(dāng)A-push到B時(shí),B中有一個(gè)Label需要顯示從A中的一個(gè)TextField輸入的內(nèi)容,這時(shí)我們需要用到正向傳值。

    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 (因?yàn)?B在A頁面提前初始化)
             B 有個(gè)屬性 string
             用B對(duì)象 給B屬性賦值(回顧下OC中KVC賦值 就理解了)
          
             這里forkey 一定要和B 屬性名字一致 (也可以用@"_string")因?yàn)槭菍傩?      */
         
         // 給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(chǔ)" 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
    

好了,到此已經(jīng)基本上介紹完頁面?zhèn)髦盗耍嘈拍銓?duì)頁面?zhèn)髦狄呀?jīng)有一定理解了吧,快去實(shí)踐吧!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,197評(píng)論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,415評(píng)論 3 415
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,104評(píng)論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,884評(píng)論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,647評(píng)論 6 408
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,130評(píng)論 1 323
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,208評(píng)論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,366評(píng)論 0 288
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,887評(píng)論 1 334
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,737評(píng)論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,939評(píng)論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,478評(píng)論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,174評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,586評(píng)論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,827評(píng)論 1 283
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,608評(píng)論 3 390
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,914評(píng)論 2 372

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