/* Just finished this app from Chapter 11 of Beginning iPhone 3 Development by Dave Mark*/
/* This app presents four text fields (Make sure you build your xib) and saves the info the a property list after you quit the app */
/* In Your ViewController.h*/
//
// _1_PersistenceViewController.h
// 11 Persistence
//
// Created by Amber Weinberg on 10/5/10.
// Copyright __MyCompanyName__ 2010. All rights reserved.
//
#import <UIKit/UIKit.h>
#define kFilename @"data.plist"
@interface _1_PersistenceViewController : UIViewController {
UITextField *field1;
UITextField *field2;
UITextField *field3;
UITextField *field4;
}
@property (nonatomic, retain) IBOutlet UITextField *field1;
@property (nonatomic, retain) IBOutlet UITextField *field2;
@property (nonatomic, retain) IBOutlet UITextField *field3;
@property (nonatomic, retain) IBOutlet UITextField *field4;
- (NSString *)dataFilePath;
- (void)applicationWillTerminate:(NSNotification *)notification;
@end
/* In Your ViewController.m*/
//
// _1_PersistenceViewController.m
// 11 Persistence
//
// Created by Amber Weinberg on 10/5/10.
// Copyright __MyCompanyName__ 2010. All rights reserved.
//
#import "_1_PersistenceViewController.h"
@implementation _1_PersistenceViewController
@synthesize field1;
@synthesize field2;
@synthesize field3;
@synthesize field4;
- (NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename];
}
- (void)applicationWillTerminate:(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];
[array release];
}
#pragma mark -
- (void)viewDidLoad {
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
field1.text = [array objectAtIndex:0];
field2.text = [array objectAtIndex:1];
field3.text = [array objectAtIndex:2];
field4.text = [array objectAtIndex:3];
[array release];
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
self.field1 = nil;
self.field2 = nil;
self.field3 = nil;
self.field4 = nil;
[super viewDidUnload];
}
- (void)dealloc {
[field1 release];
[field2 release];
[field3 release];
[field4 release];
[super dealloc];
}
@end