Datasheet
748
Add AddHotelController
IBAction is one of those cool little techniques, like IBOutlet, that does
nothing in the code but provide a way to inform Interface Builder (hence,
the IB in both of them) that this method can be used as an action for Target-
Action connections. All IBAction does is act as a tag for Interface Builder —
identifying this method (action) as one you can connect to an object
(namely, the Button) in a nib file. In this respect, this whole IBAction trick
is similar to the IBOutlet. In that case, however, you were tagging instance
variables, in this case, methods. Same difference.
I need to declare two methods — one to execute when the user taps the
Get from Contacts button, and the other for when the user taps the Save to
Contacts button.
To do that, add the bolded code in Listing 1-1 to AddHotelController.h.
Listing 1-1: AddHotelController.h
@class Trip;
@class Hotel;
@interface AddHotelController : UIViewController
<UITextFieldDelegate> {
Trip *trip;
Hotel *hotel;
IBOutlet UITextField *street;
IBOutlet UITextField *state;
IBOutlet UITextField *zip;
IBOutlet UITextField *name;
IBOutlet UITextField *city;
}
- (id) initWithHotel:(Hotel*) theHotel
trip:(Trip*) theTrip;
- (IBAction) getFromContacts:(id) sender;
- (IBAction) saveToContacts:(id) sender;
@end
You start by making the AddHotelController a UITextFieldDelegate —
it will be handing the entry of text into the text fields. As you can see, I
have had you add seven instance variables. One of them holds a reference
to Trip, and the second will hold a reference to a new Hotel object when
you do finally create one. The other five are the outlets I explained earlier.
The outlets will automatically be initialized with a pointer to the text fields
(street, state, zip, name, and city), when the application is launched
and will enable you to access the text the user has entered in those fields.
41_542934-bk07ch01.indd 74841_542934-bk07ch01.indd 748 3/23/10 11:01 PM3/23/10 11:01 PM