Datasheet

766
Entering and Saving the Hotel Information
Listing 1-13 (continued)
inManagedObjectContext:trip.managedObjectContext];
[annotation setTitle:@”Annotation”];
[annotation setHotel:hotel];
[hotel setAnnotation:annotation];
Geocoder * geocoder = [[Geocoder alloc] init];
NSString* geocodeString = [[NSString alloc ]
initWithFormat: @” %@ %@ %@ %@”, hotel.street, hotel.city,
hotel.state, hotel.zip];
CLLocationCoordinate2D theCoordinate = [geocoder
geocodeLocation:geocodeString];
hotel.annotation.latitude = [NSNumber numberWithDouble:
theCoordinate.latitude];
hotel.annotation.longitude = [NSNumber numberWithDouble:
theCoordinate.longitude];
hotel.annotation.title = hotel.name;
}
else {
[trip.managedObjectContext deleteObject:hotel];
hotel = nil;
[undoManager setActionName:
[NSString stringWithString:@”Cancel Hotel”]];
}
[self dismissModalViewControllerAnimated:YES];
}
Because you’re done with adding the new hotel, you set amEditing to NO,
so viewWillDisappear: can do its thing.
If you’re going to save the result, you set an Action Name for Undo, which
previously only dealt with adding a Hotel.
[undoManager setActionName:
[NSString stringWithString:@”Edit Hotel”]];
The rest of the code should look familiar — really, it should. Basically, you
have moved all of the code that had previously followed the creating of the
AddHotelController in insertNewObejct into this new method.
Listing 1-14 shows you the code that you need to now delete (that strike-
through stuff) or copy (that bold stuff) from insertNewObject in
HotelController.m.
For Cancel, you delete the object you had created, set the hotel instance
variable to nil, and set the Undo Manager action name.
Finally, you dismiss the modal view controller, as follows:
[self dismissModalViewControllerAnimated:YES];
41_542934-bk07ch01.indd 76641_542934-bk07ch01.indd 766 3/23/10 11:01 PM3/23/10 11:01 PM