《精通iOS开发·第六版》数据持久化基础知识之属性列表持久化

《精通iOS开发·第六版》数据持久化基础知识之属性列表持久化

1、BIDViewController.m文件

#import "BIDViewController.h"

@interface BIDViewController ()

@property(strong,nonatomic)IBOutletCollection(UITextField)NSArray *lineFields;

@end

@implementation BIDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //视图加载完成之后(通常是从nib文件加载),做一些额外的设置
    NSString *filePath = [self dataFilePath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
        for (int i=0; i<4; i++) {
            UITextField *theField = self.lineFields[i];
            theField.text = array[i];
        }
    }
    UIApplication *app = [UIApplication sharedApplication];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];
    // Do any additional setup after loading the view from its nib.
}

-(NSString *)dataFilePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
}

-(void)applicationWillResignActive:(NSNotification *)notification
{
    NSString *filePath = [self dataFilePath];
    NSArray *array = [self.lineFields valueForKey:@"text"];
    [array writeToFile:filePath atomically:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

2、BIDViewController.xib文件
这里写图片描述

注意:如下图
这里写图片描述
这里写图片描述
lineFields与XIB中的四个文本输入框链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值