ViewController.m
#import "ViewController.h"
#import "SecondViewController.h"
//遵循协议
@interface ViewController ()<SecondViewControllerDelegate>
@property(nonatomic,retain)UITextField *textField;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// Do any additional setup after loading the view, typically from a nib.
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 100, 50)];
self.textField.placeholder = @"请输入内容";
self.textField.backgroundColor = [UIColor redColor];
[self.view addSubview:self.textField];
//打开用户交互 所有view属性
self.textField.userInteractionEnabled = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 50, 100, 100);
button.backgroundColor = [UIColor orangeColor];
[button setTitle:@"Hello" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)buttonAction:(UIButton *)button{
SecondViewController *secondViewController = [[SecondViewController alloc] init];
//建立代理关系
secondViewController.delegate = self;
secondViewController.text = self.textField.text;
[self presentViewController:secondViewController animated:YES completion:nil];
}
//点击回收键面
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
-(void)getTextFieldValue:(NSString *)text{
self.textField.text = text;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
SecondViewController.h
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor brownColor];
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 40)];
lable.text = self.text;
lable.backgroundColor = [UIColor whiteColor];
[self.view addSubview:lable];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 300, 100, 50)];
self.textField.placeholder = @"请输入";
[self.view addSubview:self.textField];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(100, 200, 100, 100);
button1.backgroundColor = [UIColor orangeColor];
[button1 setTitle:@"NEST" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
}
-(void)button1Action:(UIButton *)button{
[self.delegate getTextFieldValue:self.textField.text];
[self dismissViewControllerAnimated:YES completion:nil];
}
SecondViewController.m
#import "SecondViewController.h"
@interface SecondViewController ()
@property(nonatomic,retain)UITextField *textField;
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor brownColor];
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 40)];
lable.text = self.text;
lable.backgroundColor = [UIColor whiteColor];
[self.view addSubview:lable];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 300, 100, 50)];
self.textField.placeholder = @"请输入";
[self.view addSubview:self.textField];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(100, 200, 100, 100);
button1.backgroundColor = [UIColor orangeColor];
[button1 setTitle:@"NEST" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
}
-(void)button1Action:(UIButton *)button{
[self.delegate getTextFieldValue:self.textField.text];
[self dismissViewControllerAnimated:YES completion:nil];
}
界面基本上就是上面的图,创建的特别简单,大家马马虎虎看吧 懂思想就好,
在第一个界面的textfiled上输入任意值 点击Hello的button就跳转到第二个界面,并且输入的值传到了第二界面的lable中。
同样在第二个界面的textfiled中输入任意值点击NEST就跳转到第一个界面,并且传值到第一个界面的textFiled中(这个是因为我太懒了没新建新的lable,直接在第一个界面的textFiled上传了)。