诠释完美

见贤思齐焉,见不贤而内自省也。

控制器之间的数据传递——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
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SSIrreplaceable/article/details/52357322
个人分类: iOS-UI界面设计
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭