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<SecondVCDelegete>delegate;
@end