ViewController.m
#import "ViewController.h"
#import "SecondViewController.h"
//遵循協(xié)議
@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 = @"請(qǐng)輸入內(nèi)容";
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];
//建立代理關(guān)系
secondViewController.delegate = self;
secondViewController.text = self.textField.text;
[self presentViewController:secondViewController animated:YES completion:nil];
}
//點(diǎn)擊回收鍵面
-(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 = @"請(qǐng)輸入";
[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 = @"請(qǐng)輸入";
[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];
}
屏幕快照 2017-08-15 下午7.20.36.png
屏幕快照 2017-08-15 下午7.20.44.png
界面基本上就是上面的圖窍霞,創(chuàng)建的特別簡(jiǎn)單犁嗅,大家馬馬虎虎看吧 懂思想就好,
在第一個(gè)界面的textfiled上輸入任意值 點(diǎn)擊Hello的button就跳轉(zhuǎn)到第二個(gè)界面缔赠,并且輸入的值傳到了第二界面的lable中衍锚。
同樣在第二個(gè)界面的textfiled中輸入任意值點(diǎn)擊NEST就跳轉(zhuǎn)到第一個(gè)界面,并且傳值到第一個(gè)界面的textFiled中(這個(gè)是因?yàn)槲姨珣辛藳]新建新的lable嗤堰,直接在第一個(gè)界面的textFiled上傳了)构拳。