Figure 5-2
Relationships in the target-action paradigm
myController
myController
outlet
action
In the Actions column (in the Connections pane of the inspector), all action methods are defined by the class
of the target object and known by Interface Builder. Interface Builder identifies action methods because their
names follow this syntax:
- (IBAction)myAction:(id)sender;
Note: IBAction, like IBOutlet, is a null defined macro, which the C preprocessor removes at compile time.
Interface Builder uses it to identify action declarations so that it can display them when connecting actions
visually.
Here, it looks for the argument
sender
.
Which Direction to Connect?
Usually the outlets and actions that you connect belong to a custom subclass of
NSObject
. For these
occasions, you need only to follow a simple rule to know in which way to specify a connection in Interface
Builder. Create the connection from the object that sends the message to the object that receives the message:
To make an action connection, create the connection from an element in the user interface, such as a
button or a text field, to the custom instance you want to send the message to.
To make an outlet connection, create the connection from the custom instance to another object (another
instance or user interface element) in the application.
These are only rules of thumb for common cases and do not apply in all circumstances. For instance, many
Cocoa objects have a
delegate
outlet. To connect these, you draw a connection line from the Cocoa object
to your custom object.
Another way to clarify connections is to consider who needs to find whom. With outlets, the custom object
needs to find some other object, so the connection is from the custom object to the other object. With
actions, the control object needs to find the custom object, so the connection is from the control object to
the custom object.
Paths for Object Communication: Outlets, Targets, and Actions
43
2007-10-31 | © 2007 Apple Inc. All Rights Reserved.
CHAPTER 5
Bridging the Model and View: The Controller