(一)属性传值
(二)代理(委托)传值
代理传值
适用于 反向传值 (从后往前传)
1.1 创建协议 及协议方法 在反向传值的页面(SecondViewController)中
1.2 创建协议类型的属性 在SecondViewController中创建属性id<postValueDelegate> delegate
1.3 调用属性 即delegate
在SecondViewController页面中 对象传值的方法中调用
[self.delegate postValue:self.textName.text];
1.4 在第一页 即显示修改过信息的页面
遵循协议 实现协议方法 指定代理对象(即 在页面传值参数的方法中为代理赋值)
secondVc.delegate=self;
1.2 创建协议类型的属性 在SecondViewController中创建属性id<postValueDelegate> delegate
1.3 调用属性 即delegate
在SecondViewController页面中 对象传值的方法中调用
[self.delegate postValue:self.textName.text];
1.4 在第一页 即显示修改过信息的页面
遵循协议 实现协议方法 指定代理对象(即 在页面传值参数的方法中为代理赋值)
secondVc.delegate=self;
文件目录:
FirstViewController.h
#import <UIKit/UIKit.h> #import "SecondViewController.h" @interface FirstViewController : UIViewController<UITextFieldDelegate,postValue> @property(strong,nonatomic) UITextField *textName; @property(strong,nonatomic) UIButton *nextbutton; @end
FirstViewController.m
#import "FirstViewController.h" @interface FirstViewController () @end @implementation FirstViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor redColor]; self.textName=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200,50)]; self.textName.borderStyle=UITextBorderStyleLine; self.textName.delegate=self; [self.view addSubview:self.textName]; self.nextbutton=[[UIButton alloc] initWithFrame:CGRectMake(200, 200, 100, 50)]; [self.nextbutton setTitle:@"下一页" forState:UIControlStateNormal]; self.nextbutton.backgroundColor=[UIColor grayColor]; [self.nextbutton addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.nextbutton]; } -(BOOL)textFieldShouldReturn:(UITextField *)textField { if ([textField isFirstResponder]) { [textField resignFirstResponder]; } return YES; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self.textName resignFirstResponder]; } -(void)nextPage { SecondViewController *secondVc=[[SecondViewController alloc] init]; secondVc.str=self.textName.text; secondVc.deletage=self; [self presentViewController:secondVc animated:YES completion:nil]; } -(void)postValue:(NSString *)str { self.textName.text=str; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
SecondViewController.h
#import <UIKit/UIKit.h>
// 创建协议
@protocol postValue <NSObject> -(void)postValue:(NSString *)str; @end @interface SecondViewController : UIViewController<UITextFieldDelegate> @property(strong,nonatomic) UITextField *textInfo; @property(strong,nonatomic) UIButton *backbutton; @property(strong,nonatomic) NSString *str; @property(strong,nonatomic) id<postValue> deletage; @end
SecondViewController.m
#import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor colorWithRed:0.541 green:0.878 blue:0.831 alpha:1.000]; self.textInfo=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 50)]; self.textInfo.borderStyle=UITextBorderStyleLine; self.textInfo.text=self.str; self.textInfo.delegate=self; [self.view addSubview:self.textInfo]; self.backbutton=[[UIButton alloc] initWithFrame:CGRectMake(100, 200, 100, 50)]; [self.backbutton setTitle:@"上一页" forState:UIControlStateNormal]; self.textInfo.text=self.str; [self.backbutton addTarget:self action:@selector(backPage) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.backbutton]; } -(BOOL)textFieldShouldReturn:(UITextField *)textField { if ([textField isFirstResponder]) { [textField resignFirstResponder]; } return YES; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self.textInfo resignFirstResponder]; } -(void)backPage { if (self.deletage!=0) { [self.deletage postValue:self.textInfo.text]; } [self dismissViewControllerAnimated:YES completion:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
AppDelegate.h
#import <UIKit/UIKit.h> #import"FirstViewController.h" @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
AppDelegate.m
#import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { FirstViewController *firstVc=[[FirstViewController alloc] init]; self.window.rootViewController=firstVc; return YES; }
.......
@end