[新手学IOS]第六天:持久化应用程序(1)--属性列表

1.属性列表进行序列化,其实很简答,只是文件的读入读出罢了


#import <UIKit/UIKit.h>

@interface BIDViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *field1;
@property (weak, nonatomic) IBOutlet UITextField *field2;
@property (weak, nonatomic) IBOutlet UITextField *field3;
@property (weak, nonatomic) IBOutlet UITextField *field4;

-(NSString *)dataFilePath;
//获得当前app的documents的路径.
-(void)applicationWillresignActive:(NSNotification *)notification;
//通知,这个后续介绍
-(IBAction)textFieldDoneEditing:(id)sender;
//关于键盘的消失( 第一天介绍过)

@end

.m文件的实现

#import "BIDViewController.h"

#define kFilename @"data.plist"

@interface BIDViewController ()

@end

@implementation BIDViewController

@synthesize field1,field2,field3,field4;

-(IBAction)textFieldDoneEditing:(id)sender
{
    [sender resignFirstResponder];
}

-(NSString *)dataFilePath
{
    return [
            [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:kFilename
            ];
//这一行是不是很头大,我只是把几行语句写成一个了,分分看就ok.我在上一篇关于doucument中 就知道是怎么回事了

}
//下面的代码 是 通知的一个调用函数. 我们的参数并没有使用.
-(void)applicationWillresignActive:(NSNotification *)notification
{
    NSMutableArray *array = [[NSMutableArray alloc]init];
    [array addObject:field1.text];
    [array addObject:field2.text];
    [array addObject:field3.text];
    [array addObject:field4.text];
    
    [array writeToFile:[self dataFilePath] atomically:YES];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSString *filePath = [self dataFilePath];
    if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) {
        NSArray *array = [NSArray arrayWithContentsOfFile:filePath];
        field1.text = [array objectAtIndex:0];
        field2.text = [array objectAtIndex:1];
        field3.text = [array objectAtIndex:2];
        field4.text = [array objectAtIndex:3];
    }
    
    UIApplication *app = [UIApplication sharedApplication];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationWillresignActive:) name:UIApplicationWillResignActiveNotification object:app];
    
  //这一行就是 通知中心发布一个通知了,通知的名字是参数 :name,通知的对象 是 app.调用的函数就是 
applicationWillresignActive:(NSNotification *)notification



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值