Datasheet

764
Entering and Saving the Hotel Information
Listing 1-10: Adding the Save and Cancel Buttons in viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @”Hotel Information”;
UIBarButtonItem *cancelButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@”Cancel”
style:UIBarButtonItemStyleBordered target:self
action:@selector(cancel:)];
self.navigationItem.leftBarButtonItem =
cancelButtonItem;
[cancelButtonItem release];
UIBarButtonItem *saveButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@”Save”
style:UIBarButtonItemStyleDone target:self
action:@selector(save:)];
self.navigationItem.rightBarButtonItem = saveButtonItem;
[saveButtonItem release];
}
When you created the buttons, you specified the messages that should be
sent (cancel: and save:) when the user tapped a button, and to what
object they should be sent (self).
When you specified the button style as UIBarButtonItemStyleDone, that
resulted in the familiar blue Save button being displayed. If you compile and
run RoadTrip, you’ll see that trusty Save button, but don’t tap either the
Save or Cancel button just yet because you haven’t implemented either of
their action methods. In fact, you’ll do that next.
Add the code in Listing 1-11 to AddHotelController.m to imple-
ment the cancel: method and the code in Listing 1-12 to the very same
AddHotelController.m to implement the save: method.
Listing 1-11: The cancel: Method
- (IBAction)cancel:(id)sender {
[delegate addHotelController:self didFinishWithSave:NO];
}
When the user taps the Cancel button, the cancel: message is sent to the
AddHotelController. It then sends the addHotelController;didFin
ishWithSave: message to its delegate (the HotelController). I’ll show
you how that is implemented after I explain the save: method.
41_542934-bk07ch01.indd 76441_542934-bk07ch01.indd 764 3/23/10 11:01 PM3/23/10 11:01 PM