Hour 11. Declaring Actions in an Interface File


What You’ll Learn in This Hour

Image Comparing actions and methods

Image Creating actions with Xcode

Image Connecting interface elements to actions


Introducing 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.


Paradigms and Design Patterns

Objective-C and the Cocoa frameworks take advantage of a range of development technologies that make it easy to share and reuse code as well as what is often the most expensive part of the development process: the analysis and imagination that stand behind the code that is written. These development technologies include model-view-controller, which is the overall concept behind Cocoa, as well as target-action, which is the concept that involves actions.

Concepts such as these are sometimes referred to as paradigms or design patterns. Paradigm is an English word with a long history of use in many fields. Design patterns are a concept first advanced in the late 1970s and refined over time. Some people would say that the design pattern concept fell out of favor and common use at the beginning of the 2000s. In a broader sense, paradigms and design patterns are both conventions. They add nothing to a language or framework other than limiting the ways in which they are used (and those limits can be good things).

They are akin to naming conventions for variables, methods, and classes. As specified in many programming languages, names begin with a letter and then contain letters and numbers but no spaces and, usually, no special characters other than an underscore. Spaces are not allowed. Depending on the language, the case of the letters might or might not matter (it does in Objective-C). A naming convention can limit names to perhaps two or three capital letters that identify the framework to which a class belongs (this is the convention in the Apple frameworks).

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).

What Actions Can Do for You

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.

Comparing Actions in OS X and iOS

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 Image 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.

Image

FIGURE 11.12 Add an event to an action signature.

The choices for the Arguments pop-up menu are as follows:

Image None

Image Sender

Image 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.)

Image

FIGURE 11.13 Review the debugger for the new action.

Make your choice, and Xcode does the rest.


Tip

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.


Disconnecting Actions

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.

Image

FIGURE 11.14 Remove a connection.

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.

Summary

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&A

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.

Workshop

Quiz

1. How many actions should be connected to an interface element?

2. How do you choose a name for an action?

Quiz Answers

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.

Activities

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.164.34