使用属性列表持久化应用程序

PersistenceViewController.h文件

 1 #import <UIKit/UIKit.h>
 2 
 3 #define kFileName    @"data.list"
 4 
 5 @interface PersistenceViewController : UIViewController {
 6     UITextField *field1;
 7     UITextField *field2;
 8     UITextField *field3;
 9     UITextField *field4;
10 }
11 @property (nonatomic,retain)IBOutlet UITextField *field1;
12 @property (nonatomic,retain)IBOutlet UITextField *field2;
13 @property (nonatomic,retain)IBOutlet UITextField *field3;
14 @property (nonatomic,retain)IBOutlet UITextField *field4;
15 
16 - (NSString *)dataFilePath;
17 -(void)applicationWillResignActive:(NSNotification *)notification;
18 
19 @end

 

PersistenceViewController.m

  1 #import "PersistenceViewController.h"
  2 
  3 @implementation PersistenceViewController
  4 @synthesize field1;
  5 @synthesize field2;
  6 @synthesize field3;
  7 @synthesize field4;
  8 
  9 //用于返回数据文件的完整路径名,它通过查找Document目录并对对其附加kFilename累执行该操作。
 10 -(NSString *)dataFilePath{
 11     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 12     NSString *documentsDirectory = [paths objectAtIndex:0];
 13     return [documentsDirectory stringByAppendingFormat:kFileName];
 14 }
 15 
 16 #pragma mark -
 17 
 18 /*
 19 // The designated initializer. Override to perform setup that is required before the view is loaded.
 20 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 21     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
 22     if (self) {
 23         // Custom initialization
 24     }
 25     return self;
 26 }
 27 */
 28 
 29 /*
 30 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 31 - (void)loadView {
 32 }
 33 */
 34 
 35 
 36 
 37 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
 38 //在该方法中,我们做了一下操作
 39 /**
 40     1,检查数据文件是否存在
 41     2,数据文件存在,使用该引用订阅UIApplicationWillReSignActiveNotification.
 42     3,我们使用NSNotificationCenter的默认实例,以及addObserver:select:name:object方法。我们传递一个observer即self、这就意味着
 43  PersistenceViewController是需要通知的对象。
 44  */
 45 - (void)viewDidLoad {
 46     NSString *filePath = [self dataFilePath];
 47     if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
 48         NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
 49         field1.text = [array objectAtIndex:0];
 50         field2.text = [array objectAtIndex:1];
 51         field3.text = [array objectAtIndex:2];
 52         field4.text = [array objectAtIndex:3];
 53         
 54         [array release];
 55     }
 56     
 57     UIApplication *app = [UIApplication sharedApplication];
 58     [[NSNotificationCenter defaultCenter] addObserver:self
 59                                              selector:@selector(applicationWillResignActive:)
 60                                             name:UIApplicationWillResignActiveNotification
 61                                                object:app];
 62     [super viewDidLoad];
 63 }
 64 
 65 /**
 66     
 67  */
 68 -(void)applicationWillResignActive:(NSNotification *)notification{
 69     NSMutableArray *array = [[NSMutableArray alloc] init ];
 70     [array addObject:field1.text];
 71     [array addObject:field2.text];
 72     [array addObject:field3.text];
 73     [array addObject:field4.text];
 74     
 75     [array writeToFile: [self dataFilePath] atomically:YES];
 76     [array release];
 77 }
 78 
 79 
 80 
 81 /*
 82 // Override to allow orientations other than the default portrait orientation.
 83 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 84     // Return YES for supported orientations
 85     return (interfaceOrientation == UIInterfaceOrientationPortrait);
 86 }
 87 */
 88 
 89 - (void)didReceiveMemoryWarning {
 90     // Releases the view if it doesn't have a superview.
 91     [super didReceiveMemoryWarning];
 92     
 93     // Release any cached data, images, etc that aren't in use.
 94 }
 95 
 96 - (void)viewDidUnload {
 97     // Release any retained subviews of the main view.
 98     // e.g. self.myOutlet = nil;
 99     self.field1 = nil;
100     self.field2 = nil;
101     self.field3 = nil;
102     self.field4 = nil;
103     [super viewDidUnload];
104 }
105 
106 
107 - (void)dealloc {
108     [field1 release];
109     [field2 release];
110     [field3 release];
111     [field4 release];
112     [super dealloc];
113 }
114 
115 @end

 

  

posted on 2012-05-02 13:22  生于凛冽,葬于北邙  阅读(961)  评论(0编辑  收藏  举报

导航