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