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

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

目前我所了解和掌握的傳值方式有:屬性傳值棺弊、代理傳值晶密、Block傳值、KVO傳值模她、通知傳值稻艰、單例傳值、KVC傳值侈净。

下面我們來一一看下它們究竟是怎樣進行操作和傳值的呢尊勿?

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

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

    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輸入的內(nèi)容,這時我們需要用到反向傳值义图。

    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的代理
      
      執(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" 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輸入的內(nèi)容娃承,這時我們需要用到反向傳值。

    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>
     
     // 定義一個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輸入的內(nèi)容,這時我們需要用到反向傳值匙头。

    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 漫谷,反向傳值
         //注冊觀察者,注意:觀察者的注冊和移除要對應蹂析,如果移除時發(fā)現(xiàn)沒有注冊觀察者舔示,程序會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" forState:UIControlStateNormal];
         [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
         [self.view addSubview:button];
         
     }
     
     - (void)buttonAction:(UIButton *)sender {
         // KVO
         // 把self.bTextfield.text 賦值給當前屬性
         // 在A中 監(jiān)聽 當前屬性
         self.string = self.bTextField.text;
         
         [self.navigationController popToRootViewControllerAnimated:YES];
     }
     
     @end
    
  5. 通知傳值
    用法:正向傳值
    需求:當A-push到B時碟婆,B中有一個Label需要顯示從A中的一個TextField輸入的內(nèi)容,這時我們需要用到正向傳值惕稻。

    用法:反向傳值:
    需求:A-push到B竖共,當B消失的時候,A中有一個Label需要顯示從B中的一個TextField輸入的內(nèi)容俺祠,這時我們需要用到反向傳值公给。

    我們在此將兩種傳值情況寫到一個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" forState:UIControlStateNormal];
         [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
         [self.view addSubview:button];
     }
     
     //如果是從A傳到B的話,B.m里要創(chuàng)建一個init方法,在里面寫監(jiān)聽并在里面創(chuàng)建接收容器才能成功(因為程序先執(zhí)行init方法再到viewDidLoad方法,當傳值過去時在init就開始監(jiān)聽,如果這里沒有創(chuàng)建textField接收,那就傳不過去了,所以要在init里同時創(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];
         
         // 移除某個
         // [[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. 單例傳值
    用法:正向傳值
    需求:當A-push到B時蔫缸,B中有一個Label需要顯示從A中的一個TextField輸入的內(nèi)容腿准,這時我們需要用到正向傳值。

    用法:反向傳值:
    需求:A-push到B拾碌,當B消失的時候吐葱,A中有一個Label需要顯示從B中的一個TextField輸入的內(nèi)容,這時我們需要用到反向傳值校翔。

    我們在此將兩種傳值情況寫到一個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
     
     //創(chuàng)建一個單例//如果在單線程里可以用nonatomic,如果在多線程里一定要用atomic,保證是只有一個在調(diào)用,不然在多線程里面如果多個方法調(diào)用修改單例類里的屬性時會沖突
     @property (atomic, copy) NSString *value;
     
     + (DanLi *)sharedDanLi;
     
     @end
    

    單例類.m文件:

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

    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
    

好了告希,到此已經(jīng)基本上介紹完頁面?zhèn)髦盗似私嘈拍銓撁鎮(zhèn)髦狄呀?jīng)有一定理解了吧,快去實踐吧燕偶!

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者

  • 序言:七十年代末喝噪,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子指么,更是在濱河造成了極大的恐慌酝惧,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件伯诬,死亡現(xiàn)場離奇詭異晚唇,居然都是意外死亡,警方通過查閱死者的電腦和手機盗似,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進店門哩陕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事悍及∶銎埃” “怎么了?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵心赶,是天一觀的道長扣讼。 經(jīng)常有香客問我,道長缨叫,這世上最難降的妖魔是什么椭符? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮耻姥,結果婚禮上艰山,老公的妹妹穿的比我還像新娘。我一直安慰自己咏闪,他們只是感情好,可當我...
    茶點故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布摔吏。 她就那樣靜靜地躺著鸽嫂,像睡著了一般。 火紅的嫁衣襯著肌膚如雪征讲。 梳的紋絲不亂的頭發(fā)上据某,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天,我揣著相機與錄音诗箍,去河邊找鬼癣籽。 笑死,一個胖子當著我的面吹牛滤祖,可吹牛的內(nèi)容都是我干的筷狼。 我是一名探鬼主播,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼匠童,長吁一口氣:“原來是場噩夢啊……” “哼埂材!你這毒婦竟也來了?” 一聲冷哼從身側響起汤求,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤俏险,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后扬绪,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體竖独,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年挤牛,在試婚紗的時候發(fā)現(xiàn)自己被綠了莹痢。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖格二,靈堂內(nèi)的尸體忽然破棺而出劈彪,到底是詐尸還是另有隱情,我是刑警寧澤顶猜,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布沧奴,位于F島的核電站,受9級特大地震影響长窄,放射性物質(zhì)發(fā)生泄漏滔吠。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一挠日、第九天 我趴在偏房一處隱蔽的房頂上張望疮绷。 院中可真熱鬧,春花似錦嚣潜、人聲如沸冬骚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽只冻。三九已至,卻和暖如春计技,著一層夾襖步出監(jiān)牢的瞬間喜德,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工垮媒, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留舍悯,地道東北人。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓睡雇,卻偏偏與公主長得像萌衬,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子它抱,可洞房花燭夜當晚...
    茶點故事閱讀 45,077評論 2 355

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