Datasheet

Book VII
Chapter 1
A User Interface
for Adding Hotels
and Using the
Address Book
759
Adding Some Code to Actually Add Some Functionality
Start with adding the state information it needs — a Boolean amEditing
to HotelController.h, as shown in bold in Listing 1-5.
Listing 1-5: Adding State Information to HotelController.h
@class Trip;
@class Hotel;
#import “AddHotelController.h”
@interface HotelController : UITableViewController
<NSFetchedResultsControllerDelegate,
AddHotelControllerDelegate>{
NSFetchedResultsController *fetchedResultsController;
Trip *trip;
Hotel *hotel;
NSUndoManager *undoManager;
BOOL amEditing;
}
@property (nonatomic, retain) NSFetchedResultsController
*fetchedResultsController;
@property (nonatomic, retain) NSUndoManager *undoManager;
- (void)setUpUndoManager;
- (void)cleanUpUndoManager;
@end
In viewWillDisappear:, check the state by adding the code in bold in
Listing 1-6 to viewWillDisappear: in HotelController.m.
Listing 1-6: Checking the State
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (!amEditing) {
[self cleanUpUndoManager];
// Save the context.
if (trip.managedObjectContext.hasChanges) {
NSError *error = nil;
if (![trip.managedObjectContext save:&error]) {
/* Replace this implementation with code to handle the
error appropriately. */
NSLog(@”Unresolved error %@, %@”, error,
[error userInfo]);
abort();
}
}
[trip loadHotels];
[trip.mapController refreshAnnotations];
[self resignFirstResponder];
}
}
41_542934-bk07ch01.indd 75941_542934-bk07ch01.indd 759 3/23/10 11:01 PM3/23/10 11:01 PM