Datasheet

Book VII
Chapter 1
A User Interface
for Adding Hotels
and Using the
Address Book
765
Entering and Saving the Hotel Information
Listing 1-12: The save: Method
- (IBAction)save:(id)sender {
hotel.street = street.text ;
hotel.state = state.text;
hotel.zip = zip.text;
hotel.name = name.text;
hotel.city = city.text;
[delegate addHotelController:self
didFinishWithSave:YES];
}
As you might expect, the save: message updates the Hotel object you cre-
ated in the HotelController earlier and then also sends the addHotel
Controller;didFinishWithSave: message. The difference, as you will
notice, is that, in the case of cancel, the argument is NO, and in the case of
save, the argument is YES.
Setting up the AddHotelController delegate
Here’s the deal: When it comes time to dismiss a modal view controller, the
preferred method is for the view controller that presented the modal view
controller to do the dismissing. And the preferred way to do that is delegation.
To implement that, the view controller being presented modally must define
a protocol for its delegate to implement. Stored away in this newly defined
protocol are the messages(s) that the modal view controller will send in
response to specific actions, such as taps in the Save or Cancel buttons.
The delegate needs to implement the methods and do what it needs to do to
handle either a save or a cancel, which would include — in this example —
dismissing the modal view controller.
Listing 1-13 shows the implementation of the addHotelController:did
FinishWithSave: method. Add it to HotelController.m.
Listing 1-13: addHotelController:didFinishWithSave
- (void)addHotelController:
(AddHotelController *)controller
didFinishWithSave:(BOOL)save {
amEditing = NO;
if (save) {
[undoManager setActionName:
[NSString stringWithString:@”Edit Hotel”]];
Annotation *annotation = [NSEntityDescription
insertNewObjectForEntityForName:@”Annotation”
(continued)
41_542934-bk07ch01.indd 76541_542934-bk07ch01.indd 765 3/23/10 11:01 PM3/23/10 11:01 PM