Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning iOS5 Development.pdf
Скачиваний:
7
Добавлен:
09.05.2015
Размер:
15.6 Mб
Скачать

CHAPTER 13: Basic Data Persistence

453

Figure 13–3. Designing the Persistence application’s view

Once you have all four text fields and labels placed, control-drag from the File’s Owner icon to each of the four text fields. Connect the topmost text field to the outlet called field1, the next one to field2, the third to field3, and the bottom one to field4. When you have all four text fields connected to outlets, save the changes you made to

BIDViewController.xib.

Editing the Persistence Classes

In the project navigator, select BIDViewController.m, and add the following code at the beginning of the file:

#import "BIDViewController.h"

#define kFilename

@"data.plist"

@implementation BIDViewController

@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];

www.it-ebooks.info

454CHAPTER 13: Basic Data Persistence

}

.

.

.

Then go down a bit to find the viewDidLoad and viewDidUnload methods, and fill in their contents like this:

- (void)viewDidLoad { [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

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];

}

UIApplication *app = [UIApplication sharedApplication]; [[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(applicationWillResignActive:)

name:UIApplicationWillResignActiveNotification

object:app];

}

- (void)viewDidUnload { [super viewDidUnload];

//Release any retained subviews of the main view.

//e.g. self.myOutlet = nil;

self.field1 = nil; self.field2 = nil; self.field3 = nil; self.field4 = nil;

}

Finally, add the following new method at the bottom of the file, just before @end:

- (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];

}

The first method we added, dataFilePath, returns the full pathname of our data file by finding the Documents directory and appending kFilename to it. This method will be called from any code that needs to load or save data.

- (NSString *)dataFilePath {

NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

www.it-ebooks.info

CHAPTER 13: Basic Data Persistence

455

return [documentsDirectory stringByAppendingPathComponent:kFilename];

}

In the viewDidLoad method, we do a few more things. First, we check to see if a data file already exists. If there isn’t one, we don’t want to bother trying to load it. If the file does exist, we instantiate an array with the contents of that file, and then copy the objects from that array to our four text fields. Because arrays are ordered lists, by copying them in the same order as we saved them, we are always sure to get the correct values in the correct fields.

- (void)viewDidLoad { [super 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];

}

}

After we load the data from the property list, we get a reference to our application instance and use that to subscribe to UIApplicationWillResignActiveNotification, using the default NSNotificationCenter instance and a method called addObserver:selector:name:object:. We pass an observer of self, specifying that our

BIDViewController instance should be notified. For selector, we pass a selector to the applicationWillResignActive: method, telling the notification center to call that method when the notification is posted. The third parameter, name:, is the name of the notification that we’re interested in receiving. The final parameter, object:, is the object we’re interested in getting the notification from.

UIApplication *app = [UIApplication sharedApplication]; [[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(applicationWillResignActive:)

name:UIApplicationWillResignActiveNotification

object:app];

The final new method is called applicationWillResignActive:. Notice that it takes a pointer to an NSNotification as an argument. You probably recognize this pattern from Chapter 12. applicationWillResignActive: is a notification method, and all notifications take a single NSNotification instance as their argument.

Our application needs to save its data before the application is terminated or sent to the background, so we are interested in the notification called

UIApplicationWillResignActiveNotification. This notification is posted whenever an app is no longer the one with which the user is interacting. This includes when the user quits the application and (in iOS 4 and later) when the application is pushed to the background, perhaps to later be brought back to the foreground. Earlier, in the viewDidLoad method, we used the notification center to subscribe to that particular notification. This method is called when that notification happens:

- (void)applicationWillResignActive:(NSNotification *)notification { NSMutableArray *array = [[NSMutableArray alloc] init];

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]