Datasheet

Book VII
Chapter 1
A User Interface
for Adding Hotels
and Using the
Address Book
747
Add AddHotelController
10. Make your Xcode window the active window again.
If you can’t find it, or you minimized it, just click the Xcode icon in the
Dock. The RoadTrip project should still be the active one. (You can
always tell the active project by looking at the project name at the top of
the Groups & Files list.)
This is the general pattern I use as I build my interface — add stuff, and then
simulate it to see how it really looks.
Setting up the controller
Now that you have the view set up, you need to set up the controller so that
you can
1. Get the input by first creating outlets and then connecting the outlets to
Text fields in the nib file.
2. Code methods to execute when the user selects a button and connect
them to the Round Rect Buttons — I just call them Buttons henceforth —
in the nib file.
To refresh your memory, the view controller can refer to objects created
from the nib file by using a special kind of instance variable referred to as an
outlet. If I want (for example) to be able to access the Text Field object in my
RoadTrip application, I take two steps:
1. Declare an outlet in my code by using the IBOutlet keyword.
2. Use Interface Builder to point the outlet to the text field I created earlier.
IBOutlet is a keyword that tags an instance-variable declaration so the
Interface Builder application knows that a particular instance variable is an
outlet — and can then enable the connection to it with Xcode. The fact that
a connection between an object and its outlets exists is actually stored in
a nib file. When the nib file is loaded, each connection is reconstituted and
reestablished — thus enabling you to send messages to the object.
In my code, it turns out I need to create five outlets — one to point to each
of the text fields I just set up in Interface Builder.
Then, when my application is initialized, the Text Field outlet is automati-
cally initialized with a pointer to the text field. I can then use that outlet from
within my code to get the text the user entered in the text field.
Similarly, Buttons in the nib file can be connected to methods in the view
controller by using IBAction as a return type of the method you otherwise
declare in the usual way.
41_542934-bk07ch01.indd 74741_542934-bk07ch01.indd 747 3/23/10 11:01 PM3/23/10 11:01 PM