Datasheet

770
Entering and Saving the Hotel Information
But when you displayed the AddHotelController view, there was no data
to be seen. That’s because you never copied it from the instance variables in
the hotel object to the text fields in the view.
Go ahead and do that now by adding the code in bold in Listing 1-17 to
viewDidLoad in AddHotelController.m.
Listing 1-17: Adding Default Data to the View
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @”Hotel Information”;
UIBarButtonItem *cancelButtonItem = [[UIBarButtonItem
alloc] initWithTitle:@”Cancel” style:UIBarButtonItemStyleB
ordered 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];
street.text = hotel.street;
state.text = hotel.state;
zip.text = hotel.zip;
name.text = hotel.name;
city.text = hotel.city;
If at this point you’re thinking to yourself “I understand why you may want
to have a view populated with default data under some circumstances, but
this doesn’t seem to be one of those times,” I would have to agree with you.
The reason I’m doing it is to show you how the Undo Manager keeps track of
things. I’ll leave it to you, however, to implement undo in a way that is more
appropriate to your own application.
As you saw in Figure 1-13 earlier, Undo works automatically when you enter
some data in a text field. Now you can look what happens when you enter
the data, save it, and return to the HotelController.
In Figure 1-15, I shook the iPhone and the Undo Edit Hotel dialog is displayed —
that’s because that is precisely what you set the text to say earlier in add
HotelController:didFinishWithSave: back in Listing 1-13.
Then, if I tap Undo Edit Hotel, the display reverts back to the default — Hotel
California. If I shake the device again, I get the Undo Add Hotel message, as
you see in Figure 1-16.
41_542934-bk07ch01.indd 77041_542934-bk07ch01.indd 770 3/23/10 11:01 PM3/23/10 11:01 PM