有時候我們在頁面跳轉的時候回傳遞相應的參數,如,你想把在第一個頁面的文本框里的內容顯示在第二個文本框中,或者你又想把第二個文本框中的內容改變之後到第一個頁面的文本框中,所有,這個時候我們就要用到頁面跳轉傳值 屬性傳值是正向傳值,只可以從前面一個頁面傳遞到第二個頁面,不可以從第二個頁面傳遞到第一個頁面
有時候我們在頁面跳轉的時候回傳遞相應的參數,如,你想把在第一個頁面的文本框里的內容顯示在第二個文本框中,或者你又想把第二個文本框中的內容改變之後到第一個頁面的文本框中,所有,這個時候我們就要用到頁面跳轉傳值
1.屬性傳值(正向傳值)
屬性傳值是正向傳值,只可以從前面一個頁面傳遞到第二個頁面,不可以從第二個頁面傳遞到第一個頁面
2.代理傳值(逆向傳值)
代理傳值是逆向傳值
代理傳值步驟
代理傳值
適用於 反向傳值
1.1 創建協議 及協議方法 在反向傳值的頁面(SecondViewController)中
1.2 創建協議類型的屬性 在SecondViewController中創建屬性id<postValueDelegate> delegate
1.3 調用屬性 即delegate
在SecondViewController頁面中 對象傳值的方法中調用
[self.delegate postValue:self.textName.text];
1.4 在第一頁 即顯示修改過信息的頁面
遵循協議 實現協議方法 指定代理對象(即 在頁面傳值參數的方法中為代理賦值)
具體代碼如下
firstViewController.h
#import <UIKit/UIKit.h> #import "secondViewController.h" @interface firstViewController : UIViewController<postValueDeletage,UITextFieldDelegate> @property(nonatomic,strong)UITextField *name1; @property(nonatomic,strong)UIButton *btn; @end
firstViewController.m
#import "firstViewController.h" @interface firstViewController () @end @implementation firstViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor colorWithRed:0.565 green:1.000 blue:0.994 alpha:1.000]; self.name1=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 50)]; self.name1.delegate=self; self.name1.backgroundColor=[UIColor colorWithRed:0.905 green:0.903 blue:0.910 alpha:1.000]; [self.view addSubview:self.name1]; self.btn=[[UIButton alloc]initWithFrame:CGRectMake(150, 200, 100, 100)]; [self.btn setTitle:@"點擊" forState:0]; [self.btn addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside]; self.btn.backgroundColor=[UIColor colorWithRed:0.210 green:0.257 blue:0.382 alpha:1.000]; [self.view addSubview:self.btn]; } -(void)postValue:(NSString *)str{ self.name1.text=str; } -(void)next{ secondViewController *second=[[secondViewController alloc]init]; second.deletage=self; second.str=self.name1.text; [self presentViewController:second animated:YES completion:nil]; } -(BOOL)textFieldShouldReturn:(UITextField *)textField{ if ([textField isFirstResponder]) { [textField resignFirstResponder]; } return YES; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [self.name1 resignFirstResponder]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
secondViewController.h 第二個頁面
#import <UIKit/UIKit.h> @protocol postValueDeletage <NSObject> -(void)postValue:(NSString *)str; @end @interface secondViewController : UIViewController<UITextFieldDelegate> @property(nonatomic,strong)UITextField *name1; @property(nonatomic,strong)UIButton *btn; @property(nonatomic,strong)NSString *str; @property(nonatomic,assign)id<postValueDeletage> deletage; @end
secondViewController.m
#import "secondViewController.h" @interface secondViewController () @end @implementation secondViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor colorWithRed:1.000 green:0.314 blue:0.659 alpha:1.000]; self.name1=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 50)]; self.name1.backgroundColor=[UIColor colorWithRed:0.905 green:0.903 blue:0.910 alpha:1.000]; self.name1.text=self.str; [self.view addSubview:self.name1]; self.name1.delegate=self; self.name1.text=self.str; self.btn=[[UIButton alloc]initWithFrame:CGRectMake(150, 200, 100, 100)]; [self.btn setTitle:@"上一頁" forState:0]; [self.btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; self.btn.backgroundColor=[UIColor colorWithRed:0.210 green:0.257 blue:0.382 alpha:1.000]; [self.view addSubview:self.btn]; } -(void)back{ [self.deletage postValue:self.name1.text]; [self dismissViewControllerAnimated:YES completion:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(BOOL)textFieldShouldReturn:(UITextField *)textField{ if ([textField isFirstResponder]) { [textField resignFirstResponder]; } return YES; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [self.name1 resignFirstResponder]; }
AppDelegate.h
#import <UIKit/UIKit.h> #import "firstViewController.h" @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
AppDelegate.m文件
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { firstViewController *first=[[firstViewController alloc]init]; self.window.rootViewController=first; return YES; }
3.單例傳值(雙向傳值)
首先創建一個單例類
AppStatus.h
#import <Foundation/Foundation.h> @interface AppStatus : NSObject<NSCopying> @property(nonatomic,copy)NSString *contextStr; +(AppStatus *)shareInstance; @end
AppStatus.m文件
#import "AppStatus.h" @implementation AppStatus static AppStatus *appstatus; //單例方法 +(AppStatus *)shareInstance{ if (appstatus==nil) { appstatus=[[AppStatus alloc]init]; } return appstatus; } //單例方法 初始化 +(instancetype)allocWithZone:(struct _NSZone *)zone{ if (appstatus==nil) { appstatus=[super allocWithZone:zone]; } return appstatus; } //單例方法 複製 -(id)copyWithZone:(NSZone *)zone{ return self; } @end
第一個頁面
firstViewController.h
#import <UIKit/UIKit.h> #import "sceondViewController.h" #import "AppStatus.h" @interface firstViewController : UIViewController<UITextFieldDelegate> @property(nonatomic,strong)UITextField *name1; @property(nonatomic,strong)UIButton *btn; @end
firstViewController.m
#import "firstViewController.h" @interface firstViewController () @end @implementation firstViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor colorWithRed:0.565 green:1.000 blue:0.994 alpha:1.000]; self.name1=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 50)]; self.name1.backgroundColor=[UIColor colorWithRed:0.905 green:0.903 blue:0.910 alpha:1.000]; self.name1.delegate=self; [self.view addSubview:self.name1]; self.btn=[[UIButton alloc]initWithFrame:CGRectMake(150, 200, 100, 100)]; [self.btn setTitle:@"下一頁" forState:0]; [self.btn addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside]; self.btn.backgroundColor=[UIColor colorWithRed:0.210 green:0.257 blue:0.382 alpha:1.000]; [self.view addSubview:self.btn]; } //這個方法是執行多遍的 相當於刷新view -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; //把contextStr賦值到first中文本框的內容中 self.name1.text=[AppStatus shareInstance].contextStr; } -(void)next{ sceondViewController *second=[[sceondViewController alloc]init]; //把first中文本框的內容賦值到contextStr中 [AppStatus shareInstance].contextStr=self.name1.text; NSLog(@"%@",[AppStatus shareInstance].contextStr); [self presentViewController:second animated:YES completion:nil]; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [self.name1 resignFirstResponder]; } -(BOOL)textFieldShouldReturn:(UITextField *)textField{ if ([textField isFirstResponder]) { [textField resignFirstResponder]; } return YES; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
第二個頁面sceondViewController.h
#import <UIKit/UIKit.h> #import "AppStatus.h" @interface sceondViewController : UIViewController<UITextFieldDelegate> @property(nonatomic,strong)UITextField *name1; @property(nonatomic,strong)UIButton *btn; @end
#import "sceondViewController.h" @interface sceondViewController () @end @implementation sceondViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor colorWithRed:1.000 green:0.416 blue:0.612 alpha:1.000]; self.name1=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 50)]; self.name1.backgroundColor=[UIColor colorWithRed:0.905 green:0.903 blue:0.910 alpha:1.000]; self.name1.text=[AppStatus shareInstance].contextStr; self.name1.delegate=self; [self.view addSubview:self.name1]; self.btn=[[UIButton alloc]initWithFrame:CGRectMake(150, 200, 100, 100)]; [self.btn setTitle:@"上一頁" forState:0]; [self.btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; self.btn.backgroundColor=[UIColor colorWithRed:0.210 green:0.257 blue:0.382 alpha:1.000]; [self.view addSubview:self.btn]; } -(void)back{ //把second中的文本框的值賦值給單例類的contextStr [AppStatus shareInstance].contextStr=self.name1.text; NSLog(@"%@",[AppStatus shareInstance].contextStr); [self dismissViewControllerAnimated:YES completion:nil]; } -(BOOL)textFieldShouldReturn:(UITextField *)textField{ if ([textField isFirstResponder]) { [textField resignFirstResponder]; } return YES; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [self.name1 resignFirstResponder]; } //這個方法是執行多遍的 相當於刷新view -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; //把contextStr賦值到second中文本框的內容中 self.name1.text=[AppStatus shareInstance].contextStr; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
把根視圖轉到第一個視圖,最後代碼和代理模式的一樣,在AppDelegate的
4.通知傳值(逆向傳值)
註意:通知傳值是逆向傳值,只能在第二個頁面創建通知和發送通知,在第一個頁面接收通知,並讀取通知里的信息
第一個頁面
firstViewController.h
#import <UIKit/UIKit.h> #import "secondViewController.h" @interface firstViewController : UIViewController<UITextFieldDelegate> @property(nonatomic,strong)UITextField *name1; @property(nonatomic,strong)UIButton *btn; @end
firstViewController.m
#import "firstViewController.h" @interface firstViewController () @end @implementation firstViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor colorWithRed:0.565 green:1.000 blue:0.994 alpha:1.000]; self.name1=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 50)]; self.name1.backgroundColor=[UIColor colorWithRed:0.905 green:0.903 blue:0.910 alpha:1.000]; self.name1.delegate=self; [self.view addSubview:self.name1]; self.btn=[[UIButton alloc]initWithFrame:CGRectMake(150, 200, 100, 100)]; [self.btn setTitle:@"點擊" forState:0]; [self.btn addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside]; self.btn.backgroundColor=[UIColor colorWithRed:0.210 green:0.257 blue:0.382 alpha:1.000]; [self.view addSubview:self.btn]; //添加通知 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ChangeNameNotification:) name:@"order" object:nil]; } //通知方法 -(void)ChangeNameNotification:(NSNotification *)notification{ NSDictionary *nameDic=notification.userInfo; NSLog(@"%@",nameDic); self.name1.text=[nameDic objectForKey:@"key"]; } -(void)next{ secondViewController *second=[[secondViewController alloc]init]; second.str=self.name1.text; [self presentViewController:second animated:YES completion:nil]; } //移除通知 -(void)dealloc{ [[NSNotificationCenter defaultCenter]removeObserver:self name:@"order" object:nil]; } -(BOOL)textFieldShouldReturn:(UITextField *)textField{ if ([textField isFirstResponder]) { [textField resignFirstResponder]; } return YES; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [self.name1 resignFirstResponder]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
第二個頁面secondViewController.h
#import <UIKit/UIKit.h> @interface secondViewController : UIViewController<UITextFieldDelegate> @property(nonatomic,strong)UITextField *name1; @property(nonatomic,strong)UIButton *btn; @property(nonatomic,strong)NSString *str; @end
第二個頁面secondViewController.m
#import "secondViewController.h" @interface secondViewController () @end @implementation secondViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor colorWithRed:1.000 green:0.314 blue:0.659 alpha:1.000]; self.name1=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 50)]; self.name1.backgroundColor=[UIColor colorWithRed:0.905 green:0.903 blue:0.910 alpha:1.000]; [self.view addSubview:self.name1]; self.name1.text=self.str; self.btn=[[UIButton alloc]initWithFrame:CGRectMake(150, 200, 100, 100)]; [self.btn setTitle:@"上一頁" forState:0]; [self.btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; self.btn.backgroundColor=[UIColor colorWithRed:0.210 green:0.257 blue:0.382 alpha:1.000]; [self.view addSubview:self.btn]; } -(void)back{ //創建通知 NSNotification *no=[[NSNotification alloc]initWithName:@"order" object:self userInfo:@{@"key":self.name1.text}]; //通知中心發送通知 [[NSNotificationCenter defaultCenter] postNotification:no]; [self dismissViewControllerAnimated:YES completion:nil]; } -(BOOL)textFieldShouldReturn:(UITextField *)textField{ if ([textField isFirstResponder]) { [textField resignFirstResponder]; } return YES; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [self.name1 resignFirstResponder]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /*
把根視圖轉到第一個視圖,最後代碼和代理模式的一樣,在AppDelegate的
5.代碼塊傳值(逆向傳值)
代碼塊傳值步驟
/**
* 代碼塊傳值 從後往前傳值
1.聲明代碼塊(secondXXX.h)
2.聲明一個代碼塊的類型屬性(secondXXX.h)
3.調用代碼塊(secondXXX.m)
4.實現代碼塊(FirstXXX.m)
第一個頁面firstViewController.h和
#import <UIKit/UIKit.h> #import "secondViewController.h" @interface firstViewController : UIViewController<UITextFieldDelegate> @property(nonatomic,strong)UITextField *name1; @property(nonatomic,strong)UIButton *btn; @end
#import "firstViewController.h" @interface firstViewController () @end @implementation firstViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor redColor]; self.name1=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; self.name1.borderStyle=2; self.name1.delegate=self; [self.view addSubview:self.name1]; self.btn=[[UIButton alloc]initWithFrame:CGRectMake(200, 300, 100, 100)]; [self.btn setTitle:@"下一頁" forState:0]; [self.btn addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.btn]; } //-(void)postValue:(NSString *)str{ // // self.name1.text=str; //} -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ if ([textField isFirstResponder]) { [textField resignFirstResponder]; } return YES; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [self.name1 resignFirstResponder]; } -(void)next{ secondViewController *second=[[secondViewController alloc]init]; second.str=self.name1.text; second.myBlock=^(NSString *info){ self.name1.text=info; }; // second.deletage=self; [self presentViewController:second animated:YES completion:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
第二個頁面 firstViewController.h和firstViewController.m
#import <UIKit/UIKit.h> typedef void(^postValueBlock)(NSString *info); //@protocol postValueDeletage <NSObject> // //-(void)postValue:(NSString *)str; // //@end /** * 代碼塊傳值 從後往前傳值 1.聲明代碼塊(secondXXX.h) 2.聲明一個代碼塊的類型屬性(secondXXX.h) 3.調用代碼塊(secondXXX.m) 4.實現代碼塊(FirstXXX.m) */ @interface secondViewController : UIViewController<UITextFieldDelegate> @property(nonatomic,strong)UITextField *name1; @property(nonatomic,strong)UIButton *btn; @property(nonatomic,copy)NSString *str; @property(nonatomic,strong)postValueBlock myBlock;
#import "secondViewController.h" @interface secondViewController () @end @implementation secondViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor grayColor]; self.name1=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; self.name1.borderStyle=2; self.name1.text=self.str; // self.name1.backgroundColor=[UIColor ]; [self.view addSubview:self.name1]; self.btn=[[UIButton alloc]initWithFrame:CGRectMake(200, 300, 100, 100)]; [self.btn setTitle:@"上一頁" forState:0]; [self.btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.btn]; } -(void)back{ // if (self.deletage!=nil) { // [self.deletage postValue:self.name1.text]; // // } if (self.myBlock) { self.myBlock(self.name1.text); } [self dismissViewControllerAnimated:YES completion:nil]; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [self.name1 resignFirstResponder]; } -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ if ([textField isFirstResponder]) { [textField resignFirstResponder]; } return YES; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
把根視圖轉到第一個視圖,最後代碼和代理模式的一樣,在AppDelegate的