步骤:先在iOS平台创建一个工程 ,之后再工程中创建有类(FirstVIewController)
代码实现如下
FirstViewController.h
#import <UIKit/UIKit.h>
//创建协议 声明协议
@protocol PostVslueDelegate <NSObject>
-(void)postValue:(NSString *)info;
//创建协议 声明协议
@protocol PostVslueDelegate <NSObject>
-(void)postValue:(NSString *)info;
@end
@interface FirstViewController : UIViewController<UITextFieldDelegate>
//声明属性
@property(strong,nonatomic)UITextField *textName;
@property(strong,nonatomic)NSString *string;
@property(strong,nonatomic)id<PostVslueDelegate> Delegate;
//声明属性
@property(strong,nonatomic)UITextField *textName;
@property(strong,nonatomic)NSString *string;
@property(strong,nonatomic)id<PostVslueDelegate> Delegate;
@end
FirstViewController.m
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
//设置主屏背景色
self.view.backgroundColor=[UIColor yellowColor];
//创建文本框
self.textName=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 40)];
self.textName.borderStyle=1;
//指定代理
self.textName.delegate=self;
//传值
self.textName.text=self.string;
[self.view addSubview:self.textName];
}
//实现代理方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//调用代理方法
if (self.Delegate) {
[self.Delegate postValue:textField.text];
}
//切换页面
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"切换成功");
}];
//隐藏键盘
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
}
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
ViewController.h
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface ViewController : UIViewController<UITextFieldDelegate,PostVslueDelegate>
@property(strong,nonatomic)UITextField *textName;
@property(strong,nonatomic)NSString *string;
@property(strong,nonatomic)UIButton *myButton;
@end
#import "FirstViewController.h"
@interface ViewController : UIViewController<UITextFieldDelegate,PostVslueDelegate>
@property(strong,nonatomic)UITextField *textName;
@property(strong,nonatomic)NSString *string;
@property(strong,nonatomic)UIButton *myButton;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//背景色
self.view.backgroundColor=[UIColor greenColor];
//创建文本框
self.textName=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 50)];
self.textName.borderStyle=1;
//指定代理
self.textName.delegate=self;
[self.view addSubview:self.textName];
//创建按钮
self.myButton=[[UIButton alloc]initWithFrame:CGRectMake(150, 160, 50, 50)];
self.myButton.backgroundColor=[UIColor redColor];
[self.myButton setTitle:@"Next" forState:UIControlStateNormal];
[self.myButton addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.myButton];
}
-(void)nextPage
{
FirstViewController *firstVC=[[FirstViewController alloc]init];
//正传参值
firstVC.string=self.textName.text;
//指定代理
firstVC.Delegate=self;
//切换视图
[self presentViewController:firstVC animated:YES completion:^{
NSLog(@"切换成功");
}];
}
//协议方法
-(void)postValue:(NSString *)info
{
//功能:反传参值
self.textName.text=info;
}
//代理方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//隐藏键盘
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
}
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//背景色
self.view.backgroundColor=[UIColor greenColor];
//创建文本框
self.textName=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 50)];
self.textName.borderStyle=1;
//指定代理
self.textName.delegate=self;
[self.view addSubview:self.textName];
//创建按钮
self.myButton=[[UIButton alloc]initWithFrame:CGRectMake(150, 160, 50, 50)];
self.myButton.backgroundColor=[UIColor redColor];
[self.myButton setTitle:@"Next" forState:UIControlStateNormal];
[self.myButton addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.myButton];
}
-(void)nextPage
{
FirstViewController *firstVC=[[FirstViewController alloc]init];
//正传参值
firstVC.string=self.textName.text;
//指定代理
firstVC.Delegate=self;
//切换视图
[self presentViewController:firstVC animated:YES completion:^{
NSLog(@"切换成功");
}];
}
//协议方法
-(void)postValue:(NSString *)info
{
//功能:反传参值
self.textName.text=info;
}
//代理方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//隐藏键盘
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
}
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
效果图
1、从第一个视图传参(The only)到下一个视图(正向传参)
1、从视图传参(The only one of)到上一个视图(逆向传参)