控制器之间的数据传递——UIApplication传值

UIApplication传值

我们先在这里约定:界面1传值到界面2为顺传,界面2传值到界面1为逆传

[UIApplication sharedApplication]可以获取到一个UIApplication对象,该对象代表当前应用程序,是一个单例对象

一. 实现步骤

  1. 在AppDelegate.h中添加一个属性用于保存传递的数据

  2. 在界面2中,用[[UIApplication sharedApplication] delegate]获取UIApplication的代理,并把值给代理

  3. 在界面1中,也用[[UIApplication sharedApplication] delegate]获取UIApplication的代理,然后获取所保存的值

二. 具体代码

1. AppDelegate类

---------- AppDelegate.h文件

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

// 用于保存视图控制器之间所要传递的值
@property (nonatomic,strong) NSString *passData;

@end

---------- AppDelegate.m文件

#import "AppDelegate.h"
#include "OneViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  // 1. 创建窗口
  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

  // 2. 创建窗口的根控制器
  // 2.1 创建导航控制器的根控制器
  UIViewController *oneVc = [[OneViewController alloc] init];
  // 2.2 创建导航控制器
  UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:oneVc];
  // 2.3 给窗口设置根控制器
  self.window.rootViewController = nav;

  // 3. 设置窗口为主窗口并显示窗口
  [self.window makeKeyAndVisible];

  // 隐藏导航控制器的导航条
  nav.navigationBarHidden = YES;

  return YES;
}

@end

2. OneViewController类

---------- OneViewController.m文件

#import "OneViewController.h"
#import "TwoViewController.h"
#import "AppDelegate.h"

@interface OneViewController ()

@property (nonatomic,strong) UITextField *textField;

@end

@implementation OneViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  //设置控制器View的背景颜色
  self.view.backgroundColor = [UIColor greenColor];

  // 创建点击按钮
  UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  clickBtn.frame = CGRectMake(10, 100, 80, 40);
  clickBtn.backgroundColor = [UIColor whiteColor];
  [clickBtn setTitle:@"到界面2" forState:UIControlStateNormal];
  [clickBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  [clickBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
  [clickBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:clickBtn];

  // 创建文本框
  _textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 150, 250, 40)];
  _textField.borderStyle = UITextBorderStyleRoundedRect;
  _textField.backgroundColor = [UIColor whiteColor];
  [self.view addSubview:_textField];

}

#pragma mark - 点击事件

- (void)btnClick {

  // 创建界面2,并压入栈
  TwoViewController *twoVc = [[TwoViewController alloc] init];
  [self.navigationController pushViewController:twoVc animated:YES];

}

- (void)viewWillAppear:(BOOL)animated {

  // 获取应用程序的代理
  AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];

  // 获取数据
  _textField.text = app.passData;

}

@end

3. TwoViewController类

---------- TwoViewController.m文件

#import "TwoViewController.h"
#import "AppDelegate.h"

@interface TwoViewController ()

@property (nonatomic,strong) UITextField *textField;

@end

@implementation TwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];

  //设置控制器View的背景颜色
  self.view.backgroundColor = [UIColor yellowColor];

  // 创建点击按钮
  UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  clickBtn.frame = CGRectMake(10, 100, 80, 40);
  clickBtn.backgroundColor = [UIColor whiteColor];
  [clickBtn setTitle:@"传 值" forState:UIControlStateNormal];
  [clickBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  [clickBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
  [clickBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:clickBtn];

  // 创建文本框
  _textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 150, 250, 40)];
  _textField.borderStyle = UITextBorderStyleRoundedRect;
  _textField.backgroundColor = [UIColor whiteColor];
  [self.view addSubview:_textField];
}

#pragma mark - 点击事件

- (void)btnClick {

  // 获取应用程序的代理
  AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];

  // 保存要传递的数据
  app.passData = _textField.text;

  // 跳转界面
  [self.navigationController popViewControllerAnimated:YES];

}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值