Datasheet
Book VII
Chapter 1
A User Interface
for Adding Hotels
and Using the
Address Book
755
Adding Some Code to Actually Add Some Functionality
- (id) initWithHotel:(Hotel*) theHotel trip:(Trip*) theTrip
{
if (self = [super initWithNibName:@”AddHotelController”
bundle:nil]) {
hotel = theHotel;
trip = theTrip;
}
return self;
}
This is your run-of-the-mill initialization method, and there really isn’t any-
thing left to say about it, other than you’d better not forget the #import
statements, because the compiler will be happy to indirectly point out to
you that they’re missing. In this particular initialization method, you save a
reference to the Trip (which you won’t be using, but I have you do it now
because as you build out the application, you’re likely to need it), and you
also save a reference to the Hotel object that was created and added to
the Managed Object Context in the HotelController object’s insert
NewObject method and which you’ll update based on what the user enters.
To actually display the AddHotelController, you need to add some code
to HotelController. So, go ahead and add the bolded code in Listing 1-3
to the aforementioned HotelController that will create and then push the
AddHotelController.
Listing 1-3: Modifying InsertNewObject
- (void)insertNewObject {
NSEntityDescription *entity = [[fetchedResultsController
fetchRequest] entity];
hotel = [NSEntityDescription insertNewObjectForEntit
yForName:[entity name] inManagedObjectContext:trip.
managedObjectContext]; //$$
[self setUpUndoManager];
[hotel setValue:@”Hotel California” forKey:@”name”];
[hotel setValue:@”1 Dr. Carlton B. Goodlett Place”
forKey:@”street”];
[hotel setValue:@”San Francisco” forKey:@”city”];
[hotel setValue:@”California” forKey:@”state”];
[hotel setValue:@”94102” forKey:@”zip”];
AddHotelController *addHotelController =
[[AddHotelController alloc] initWithHotel:hotel
trip:trip];
UINavigationController *navigationController =
[[UINavigationController alloc] initWithRootViewController
:addHotelController];
navigationController.modalTransitionStyle =
UIModalTransitionStyleFlipHorizontal;
(continued)
41_542934-bk07ch01.indd 75541_542934-bk07ch01.indd 755 3/23/10 11:01 PM3/23/10 11:01 PM