When I began writing my book, iOS did not have multitasking. By the time the book was almost complete, iOS4 had arrived. One of the side effects of multitasking was that when you quit an app, it would enter background, and because of this the SimplePreference app may not appear to work properly. Since the app no longer terminated, we now have to allow for the app going into and coming from the background.
We will now monitor the following notification, by registering for the notification in the viewDidLoad method:
UIApplicationWillEnterForegroundNotification
Here is a modification to the RootViewController that will now read the preferences when entering foreground.
Code:
//
// RootViewController.m
// SimplePreference
//
// Created by Gene Backlin on 5/16/10.
// Copyright MariZack Consulting 2010. All rights reserved.
//
#import "RootViewController.h"
#import "AppPreferences.h"
NSString *kFirstNamePreferenceKey = @"first_name_preference";
NSString *kLastNameLPreferenceLKey = @"last_name_preference";
NSString *kContactPreferenceLKey = @"contact_preference";
@implementation RootViewController
@synthesize appPreferences;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(becomeActive:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
[self retrieveAppPreferences];
}
- (void)retrieveAppPreferences {
[self setAppPreferences:[[AppPreferences alloc]
initWithKeys:[NSArray arrayWithObjects:
kFirstNamePreferenceKey,
kLastNameLPreferenceLKey,
kContactPreferenceLKey,
nil]]];
[self setTitle:[[self appPreferences]
valueForKey:kContactPreferenceLKey]];
[[self tableView] reloadData];
}
- (void)becomeActive:(NSNotification *)notification {
[self retrieveAppPreferences];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
#pragma mark -
#pragma mark Table view data source
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[[self appPreferences] keys] count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger row = [indexPath row];
NSArray *keys = [[[self appPreferences] keys]
sortedArrayUsingSelector:
@selector(localizedCaseInsensitiveCompare:)];
NSString *key = [keys objectAtIndex:row];
NSString *keyValue = [[self appPreferences] valueForKey:key];
NSString *cellText = [NSString stringWithFormat:@"%@=%@", key, keyValue];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
[[cell textLabel] setText:cellText];
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[self setAppPreferences:nil];
}
- (void)dealloc {
[super dealloc];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[appPreferences release];
}
@end
Enjoy !!!