What You’ll Learn in This Hour
Comparing actions and methods
Creating actions with Xcode
Connecting interface elements to actions
Hour 10, “Declaring Methods in an Interface File,” covered the basics of declaring methods. There is a great deal of flexibility in the Objective-C method declaration structure, and you can use it to make your code easy to read and reuse (not to mention easy for users to use). In this hour, you learn how to create and use actions—a special type of method that is a key part of the Cocoa frameworks. Actions make it possible to connect interface elements such as buttons and menu commands to code that is triggered when the action is invoked by a user. In a very real sense, almost the entire user interface of Cocoa and Cocoa Touch rests on actions.
Without a doubt, actions are part of the Cocoa frameworks; however, they are implemented by writing Objective-C code, and their interactions with interface elements are created in most cases using Xcode. This hour shows you the basics of actions in both the frameworks and in the language contexts.
It might seem a little strange to have both the very flexible structure for building methods you saw in Hour 10 as well as a far more specialized structure to build actions. The reason for this is that by having this highly specialized structure for actions, you gain a great deal of flexibility in developing apps based on Cocoa and Cocoa Touch.
Regardless of the status of the terminology, the specific concepts are easy to implement in Objective-C and the Cocoa frameworks, and they are widely used in those contexts.
The easiest way to introduce actions is to demonstrate how you can use them to build an app with a graphical user interface that requires next to no programming. Much of the work is done by dynamic and runtime features of Objective-C as well as by the Cocoa frameworks. A key part of the demonstration involves the target-action paradigm (or design pattern, if you prefer that term).
To demonstrate what actions can do for you, the developer, you will see how to build an OS X app and a comparable iOS app with next to no programming. It all relies on actions and Interface Builder in Xcode.
Note: Where to Connect Your Actions
In the OS X Cocoa Application template, you connect your actions to AppDelegate. In the iOS Single View Application template, you connect your actions to ViewController. Both of these are shown in the steps that follow. As you explore Cocoa and Cocoa Touch, you will find other objects to which you can connect actions, but that is beyond the scope of this book. The process, however, is exactly the same.
You have not actually written any code at this point. You have used a totally graphical interface to build your app and its interface. The only code you need to write is the app-specific implementation of the experiment command.
Once again, without writing code, you have wired up an interface element to an action. In this case, it is a button rather than a menu command, but the process is the same.
As you have seen, actions are a specific type of method that are used primarily to implement the consequence of a user’s interaction with the user interface—often choosing a menu command or clicking a button or other interface control. Although you are free to name your action what you want, the format of its header is precise, which is one of the reasons it is easy to build the graphical user interface of Interface Builder.
GO TO Hour 12, “Routing Messages with Selectors,” p. 165, to learn more about how actions are dispatched.
The header for an action looks like this in both OS X and iOS:
– (IBAction)experimentButton:(id)sender;
As you see in the debugger at the bottom of Figure 11.11, although sender
is typed as id
(a generic object), when it is passed into the action, the actual type of the object is available (a UIButton
in this case). This is the standard behavior.
Tip
An id
can be of any type, but at runtime its actual type is available to you.
As noted previously, IBAction
is used by Interface Builder to help you build the interface. It signifies an action that can be connected to a control. The compiler defines IBAction
as void
, so by compile time this looks like a standard method.
In iOS, there are two variants on action signatures in addition to the one shown previously. You need not pass in a sender, but you can also pass in both a sender and an event as in the following methods:
– (IBAction)experimentButton2:(id)sender forEvent:(UIEvent *)event;
– (IBAction)experimentButton3;
You choose the type of action you want to create from the small dialog box in which you also name the action. Figure 11.12 provides a close-up view of this dialog box if you follow the previous steps to create a second button and a new action called experimentButton2
. In this example, both the sender and event are chosen.
The choices for the Arguments pop-up menu are as follows:
None
Sender
Sender and Event
Set a breakpoint on the new action method and run the app. As you can see in the debugger at the bottom of Figure 11.13, this enables you to see both the object that was acted upon and what the action was. In the case of Figure 11.13, it is the UIButton
that is the sender, and the event is UITouchesEvent
. (Those lines in the debugger are highlighted.)
Make your choice, and Xcode does the rest.
If you choose one of the signatures that includes sender
, you can check sender
to see which interface element has prompted the action. You can then provide variations within the action.
You have seen how to connect an interface element to an action that you create in Xcode. Here’s how to do the reverse: disconnect an action.
Select the object that is currently connected (it might be a UIButton
). Open the Connections inspector in the utility area, as shown in Figure 11.14.
You see the connection to your action in Figure 11.14. To its left, there is a small X. Click it to remove the connection. You can now create a new connection to a new or existing action.
In this hour, you have seen how to create actions—the special type of methods that provide most of an app’s functionality. You’ve also seen how to connect interface elements such as buttons to those actions.
For many developers, these features (actions, interface elements, and the connections between them) represent the major part of an app’s design and development.
Q. What makes actions different from methods?
A. Actions use a constrained version of general method syntax. This makes it easy for Xcode to implement automated tools to create actions. It also makes it easier to dispatch varied actions at runtime.
Q. What is one of the reasons why iOS actions can have both a sender and an event?
A. On OS X, user events for controls are primarily mouse clicks. With Cocoa Touch, a wider variety of taps and gestures can be used. The event argument lets you find out more about how an action was triggered.
1. How many actions should be connected to an interface element?
2. How do you choose a name for an action?
1. At least one action should be connected to an interface element if it is enabled. Remember to differentiate enabled from disabled interface elements so that users don’t wander around clicking on everything on the screen.
2. Use the standard naming convention for methods that you have adopted for your project. Action names are not subject to additional constraints; it is the arguments that must adhere to specific names and types.
Use the examples in this hour to build an interface with menus (OS X) or controls such as buttons (iOS). This is a good way to start exploring the Cocoa frameworks with the debugger and, if you want to, with interface elements such as text fields. They all use the same basic pattern: Create an object such as a button or menu command and Control-drag to the interface file. This hour showed you how to create actions. Just choose Outlet instead of Action in the small dialog and you will have created a property that you can use to modify the behavior and appearance of the interface element.
3.17.164.34