关闭

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

标签: 文件传值界面传值控制器传值偏好设置传值偏好设置
337人阅读 评论(0) 收藏 举报
分类:

NSUserDefaults传值

使用偏好设置(NSUserDefaults)传值实质是一个控制器把数据持久化于沙盒,另一个控制器再从沙盒读取对应的文件获取数据,它是一种以文件进行传值的方式。

一. 实现步骤

  1. 在界面2中,声明NSUserDefaults变量,并往NSUserDefaults对象中添加要传递的数据

  2. 在界面1中,声明NSUserDefaults变量,通过对应的健取出对应的数据

二. 具体代码

1. AppDelegate类

---------- 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"

@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 {

   // 创建NSUserDefaults单例对象,它可以把数据以Plist文件形式存储于沙盒中,也可从沙盒中读取对应的Plist文件
  NSUserDefaults *passData = [NSUserDefaults standardUserDefaults];
  //获取数据
  _textField.text = [passData objectForKey:@"passData"];

}

@end

3. TwoViewController类

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

#import "TwoViewController.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 {

  // 创建NSUserDefaults单例对象,它可以把数据以Plist文件形式存储于沙盒中,也可从沙盒中读取对应的Plist文件
  NSUserDefaults *passData = [NSUserDefaults standardUserDefaults];
  // 保存数据
  [passData setObject:_textField.text forKey:@"passData"];

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

}

@end
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:41748次
    • 积分:1134
    • 等级:
    • 排名:千里之外
    • 原创:73篇
    • 转载:7篇
    • 译文:0篇
    • 评论:8条