新建一个空工程 在appdelegate中的.h填写以下代码app
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; //保存字符串 @property (nonatomic,copy) NSString *labelStr; @end
在RootViewController以下代码:atom
#import "RootViewController.h" #import "MyControl.h" #import "SecondViewController.h" #import "AppDelegate.h" #define kDebugPrint NSLog(@"%s",__func__) @interface RootViewController () { UILabel *_label; } @end @implementation RootViewController /* 正向传值 建立第一个界面 经过第一个界面跳转到第二个界面 若是由第一个界面向第二个界面 进行传值 正向传值 属性传值 第二张向第一张界面传值 反向传值 下级界面向上一级界面传值---》反向传值 反向传值方式: 1.代理传值 下级界面要把textField的内容 传给 上一级,这时下级界面就能够委托上级界面 修改 label的值 第二个界面(主动方) 能够制定一个协议 规范代理的行为, 第一个界面(被动方) 遵照协议 做为 代理 2.单例传值 1.系统单例 2.自定义单例 UIApplication 单例传值 第二个界面 把值给单例 第一个从单例中获取值 3.通知传值 4.NSUserDefaults 5.block传值 */ - (void)viewDidLoad { [super viewDidLoad]; [self showUI]; } - (void)showUI { self.view.backgroundColor = [UIColor grayColor]; _label = [MyControl creatLabelWithFrame:CGRectMake(0, 30, 300, 30) text:@"XXX"]; _label.backgroundColor = [UIColor yellowColor]; [self.view addSubview:_label]; UIButton *button = [MyControl creatButtonWithFrame:CGRectMake(10, 200, 300, 50) target:self sel:@selector(btnClick:) tag:201 image:nil title:@"切换到第二张"]; [self.view addSubview:button]; } //界面将要显示的时候 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; //将要显示的时候 从单例中获取 值 UIApplication *app = [UIApplication sharedApplication]; AppDelegate *appDelegate = app.delegate; //修改值 _label.text = appDelegate.labelStr; } - (void)btnClick:(UIButton *)btn { //每次点击按钮 都会建立一个新的第二张对象 SecondViewController *svc = [[SecondViewController alloc] init]; [self presentViewController:svc animated:YES completion:nil]; [svc release]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
在SecondViewController以下代码代理
#import "SecondViewController.h" #import "MyControl.h" #define kDebugPrint NSLog(@"%s",__func__) #import "AppDelegate.h" @interface SecondViewController () { UITextField *_textField; } @end @implementation SecondViewController - (void)dealloc { kDebugPrint; [super dealloc]; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor yellowColor]; [self showUI]; } - (void)showUI { UIButton *button = [MyControl creatButtonWithFrame:CGRectMake(10, 30, 300, 30) target:self sel:@selector(btnClick:) tag:301 image:nil title:@"返回"]; [self.view addSubview:button]; UIButton *button2 = [MyControl creatButtonWithFrame:CGRectMake(10,200 , 300, 30) target:self sel:@selector(btnClick2:) tag:302 image:nil title:@"传值"]; [self.view addSubview:button2]; _textField = [MyControl creatTextFieldWithFrame:CGRectMake(10, 100, 300, 30) placeHolder:nil delegate:nil tag:100]; [self.view addSubview:_textField]; } //收键盘 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [_textField resignFirstResponder]; } - (void)btnClick:(UIButton *)btn { //返回上一级 [self dismissViewControllerAnimated:YES completion:nil]; } - (void)btnClick2:(UIButton *)btn { //点击传值按钮先把 数据给 单例 //UIApplication //获取单例 UIApplication *app = [UIApplication sharedApplication]; //获得AppDelegate AppDelegate *appDelegate = app.delegate; //传值 把值 赋给appDelegate的属性 appDelegate.labelStr = _textField.text; } @end