下面的代码如下,正向传值是用普通的传值模式,反向传值用的是代理模式。实现代理模式的步骤如下:
代理方:
1.遵守原则 (遵守协议)
2.实现代理方法 (实现协议中的方法)
3.设置委托方的代理人
委托方 :
1.制定原则 (协议)
2.声明代理的属性(协议类型的属性) delegate
3.在适当的时机,让代理人执行代理方法 (delegate调用协议中的方法)
第一个界面的头文件和实现文件如下:
#import <UIKit/UIKit.h> @interface OneViewController : UIViewController @end
// // OneViewController.m // 双向传值 // // Created by spare on 16/4/16. // Copyright © 2016年 spare. All rights reserved. // //代理方: //1.遵守原则 (遵守协议) //2.实现代理方法 (实现协议中的方法) //3.设置委托方的代理人 #import "OneViewController.h" #import "TwoViewController.h" //1.遵守原则 (遵守协议) @interface OneViewController ()<TwoDelegate>@property (weak, nonatomic) IBOutlet UITextField *textField1; @end @implementation OneViewController //2.实现代理方法 (实现协议中的方法) -(void)sendTwoController:(TwoViewController *)twoController message:(NSString *)message{ //将从页面2返回的值,传到页面1的textField self.textField1.text=message; } //点击屏幕任意位置,进入页面2 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //初始化将要进入的页面 TwoViewController *vc2=[[TwoViewController alloc]init]; //正向传值代码:通过页面2定义的Content属性,将值传给页面2 vc2.content=self.textField1.text; //3.设置委托方的代理人 //反向传值代码:设置代理(很重要!!!) vc2.delegate=self; [self presentViewController:vc2 animated:YES completion:nil]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
第二个界面的头文件和实现文件如下:
// TwoViewController.h // 双向传值 // // Created by spare on 16/4/16. // Copyright © 2016年 spare. All rights reserved. // ////委托方 : //1.制定原则 (协议) //2.声明代理的属性 (协议类型的属性) delegate //3.在适当的时机,让代理人执行代理方法 (delegate 调用协议中的方法) #import <UIKit/UIKit.h> @class TwoViewController; //1.制定原则 (协议) @protocol TwoDelegate <NSObject> -(void)sendTwoController:(TwoViewController*)twoController message:(NSString*)message; @end @interface TwoViewController : UIViewController @property(nonatomic,copy)NSString *content; //2.声明代理的属性 (协议类型的属性) delegate @property(nonatomic,strong)id<TwoDelegate> delegate; @end
// // TwoViewController.m // 双向传值 // // Created by spare on 16/4/16. // Copyright © 2016年 spare. All rights reserved. // #import "TwoViewController.h" @interface TwoViewController () @property (weak, nonatomic) IBOutlet UITextField *textField2; @end @implementation TwoViewController - (void)viewDidLoad { [super viewDidLoad]; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //3.在适当的时机,让代理人执行代理方法 (delegate 调用协议中的方法) [self.delegate sendTwoController:self message:self.textField2.text]; [self dismissViewControllerAnimated:YES completion:nil]; } //每次进入页面的时候,都去刷新获得的值 -(void)viewWillAppear:(BOOL)animated{ //通过中间属性,传值给textField2; self.textField2.text=self.content; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end