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的上一個頁面(控制器)元扔。
-
屬性傳值
用法:正向傳值
需求:當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
-
代理傳值
用法:反向傳值:
需求: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
-
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
-
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
-
通知傳值
用法:正向傳值
需求:當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
-
單例傳值
用法:正向傳值
需求:當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
-
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)有一定理解了吧,快去實踐吧燕偶!