iOS 頁面?zhèn)髦抵嗌??你真的了解嗎?/h2>

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. 注冊觀察者
             2. KVO的回調(diào)
             3. 移除觀察者
          
          */
         
         // B 傳到 A ,反向傳值
         //注冊觀察者,注意:觀察者的注冊和移除要對應(yīng),如果移除時(shí)發(fā)現(xiàn)沒有注冊觀察者,程序會(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í)我們需要用到反向傳值。

    我們在此將兩種傳值情況寫到一個(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í)我們需要用到反向傳值。

    我們在此將兩種傳值情況寫到一個(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對象 給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)髦盗?,相信你對頁面?zhèn)髦狄呀?jīng)有一定理解了吧,快去實(shí)踐吧!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容