Graphics Programs Reference
In-Depth Information
{
if ([self isEastOfTheMississippi])
return @"Buying supplies"
return @"On the Oregon Trail, uncharted territory";
}
We can think of a protocol as a contract, whereby the conforming class says, “I promise to
give you my interpretation of this contract when asked.” The objects that speak to the con-
forming class through the protocol honor this contract by saying, “I promise to only ask
you things in this contract.” For example,
MKAnnotationView
has an
annotation
property declared as
@property (nonatomic, retain) id <MKAnnotation> annotation;
This declaration says that the
annotation
can be of any type (
id
), as long as it con-
forms to the
MKAnnotation
protocol (<
MKAnnotation
>). Therefore, the
MKAn-
notationView
will only send messages from the
MKAnnotation
protocol to its
an-
notation
; it won't make any assumptions about the other messages that object might
respond to.
You've added a lot of code, so you may want to build the application (Command-B) to
check for syntax errors before you continue. There's no need to run it, though, because the
application's behavior hasn't changed.
Tagging locations
Now that you have a class that conforms to
MKAnnotation
, you can display instances
of it on the map. The user will enter the location's name in the
UITextField
and then
tap the
Done
button on the keyboard. The tapping of the
Done
button is the signal to add
an annotation. How will we know this event has occurred? Delegation, again.
In the XIB file, you set the text field's
delegate
to be the instance of
Wheream-
iViewController
. This means you can send
WhereamiViewController
mes-
sages in the
UITextFieldDelegate
protocol. One of these methods is
tex-
tFieldShouldReturn:
. When the keyboard's return key is tapped, the
UITex-
tField
sends this message to its delegate and asks if it really should return. In this meth-
od, the delegate has the opportunity to perform tasks that should coincide with the return-
ing of the text field.