AppDelegate.m
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//创建window
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
//根视图控制器
self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[FirstViewController new]];
//显示控制器
[self.window makeKeyWindow];
return YES;
}
FirstViewController.m
#import "FirstViewController.h"
#import "SecondViewController.h"
//遵循SecondVCDelegate代理
@interface FirstViewController ()@property(nonatomic,strong)UITextField *textField;
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor cyanColor];
[self creatUI];
}
-(void)creatUI{
//方法一
//self.textField = [UITextField new];
//方法二
self.textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 30)];
self.textField.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.textField];
UIButton *pushButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
pushButton.frame = CGRectMake(0, 150, self.view.frame.size.width, 30);
[pushButton setTitle:@"push" forState:(UIControlStateNormal)];
[pushButton setTitleColor:[UIColor yellowColor] forState:(UIControlStateNormal)];
//点击事件
[pushButton addTarget:self action:@selector(PushClick:) forControlEvents:UIControlEventTouchUpInside];
//添加button
[self.view addSubview:pushButton];
}
//按钮点击事件
-(void)PushClick:(UIButton *)sender{
SecondViewController *secondVC = [SecondViewController new];
//给二级页面设置代理人(一级页面)
secondVC.delegate = self;
//进入SecondViewController
[self.navigationController pushViewController:secondVC animated:YES];
}
//代理人实现协议方法
-(void)changValue:(NSString *)name{
self.textField.text = name;
}
- (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
#import#import "SecondViewController.h"/* 代理传值 -- > 反向传值(二传一) 从后向前传值 二级页面定义协议和声明代理,一级页面确认并实现代理,一级页面就为二级页面的代理 *///第一步:在二级页面中//1.定义协议//2.设置协议中的方法//3.声明代理//4.在实现中为其绑定方法#warning 声明协议@protocol SecondVCDelegate-(void)changValue:(NSString *)name;@end@interface SecondViewController : UIViewController//声明属性@property(nonatomic,assign)iddelegate;
@end
SecondViewController.m
#import "SecondViewController.h"
@interface SecondViewController ()
@property(nonatomic,strong)UITextField *textField2;
//let KscreenWidth = UIScreen.main.bound.size.width
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor purpleColor];
// self.view.backgroundColor = [UIColor yellowColor];
// UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 100,self.view.frame.size.width, 30)];
//
//
// lable.backgroundColor = [UIColor whiteColor];
// [self.view addSubview:lable];
[self creatUI];
}
-(void)creatUI{
//方法一
//self.textField = [UITextField new];
//方法二
self.textField2 = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 30)];
self.textField2.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.textField2];
UIButton *pushButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
pushButton.frame = CGRectMake(0, 150, self.view.frame.size.width, 30);
[pushButton setTitle:@"pop" forState:(UIControlStateNormal)];
[pushButton setTitleColor:[UIColor yellowColor] forState:(UIControlStateNormal)];
//点击事件
[pushButton addTarget:self action:@selector(PopClick:) forControlEvents:UIControlEventTouchUpInside];
//添加button
[self.view addSubview:pushButton];
}
//实现button方法
-(void)PopClick:(UIButton *)button{
//让代理人执行协议方法
[self.delegate changValue:self.textField2.text];
//返回上一页面
[self.navigationController popViewControllerAnimated:YES];
}
- (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
#import "SecondViewController.h"
/* 代理传值 -- > 反向传值(二传一) 从后向前传值 二级页面定义协议和声明代理,一级页面确认并实现代理,一级页面就为二级页面的代理 *///第一步:在二级页面中//1.定义协议//2.设置协议中的方法//3.声明代理//4.在实现中为其绑定方法
#warning 声明协议
@protocol SecondVCDelegate-(void)changValue:(NSString *)name;
@end
@interface SecondViewController : UIViewController//声明属性@property(nonatomic,assign)id
@end