Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Mobile Development > BOOK: Professional iPhone and iPad Application Development
|
BOOK: Professional iPhone and iPad Application Development
This is the forum to discuss the Wrox book Professional iPhone and iPad Application Development by Gene Backlin; ISBN: 978-0-470-87819-4
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional iPhone and iPad Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 26th, 2011, 11:32 PM
Wrox Author
 
Join Date: Oct 2010
Posts: 61
Thanks: 0
Thanked 9 Times in 7 Posts
Default Chapter 9 - Application going into background - SimplePreference

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 !!!
 
Old February 28th, 2011, 12:00 AM
Wrox Author
 
Join Date: Oct 2010
Posts: 61
Thanks: 0
Thanked 9 Times in 7 Posts
Default

For the ChildPanePreference application the RootViewController will be modified as follows:

Code:
//
//  RootViewController.m
//  ChildPanePreference
//
//  Created by Gene Backlin on 5/17/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 *kAccountTypePreferenceLKey = @"account_type_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, 
                                                     kAccountTypePreferenceLKey, 
                                                                                            nil]]];
	[self setTitle:[[self appPreferences] 
                                        valueForKey:kAccountTypePreferenceLKey]];
	[[self tableView] reloadData];
}

- (void)becomeActive:(NSNotification *)notification {
	[self retrieveAppPreferences];
}


#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 {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
	[self setAppPreferences:nil];
}


- (void)dealloc {
    [super dealloc];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [appPreferences release];
}

@end





Similar Threads
Thread Thread Starter Forum Replies Last Post
background-color vs background-image cangrejero CSS Cascading Style Sheets 1 February 20th, 2011 09:39 PM
Chapter 3 Application Class zmanc BOOK: Professional Android 2 Application Development 0 December 14th, 2010 03:35 PM
Chapter 14: Pages 491-492: Part of row doesn't show the black background gfmann BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 14 March 3rd, 2010 10:42 AM
Chapter 4 Compass background color eags BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 1 January 10th, 2009 01:07 PM
running application in background. akhilhp Visual Basic 2005 Basics 3 September 7th, 2008 10:19 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.