Target/Action

The target/action pattern is part of the mechanism by which user interface controls respond to user actions, enabling users to communicate their intentions to an application. The target/action pattern specifies a one-to-one relationship between two objects; the control (more specifically, the control’s cell) and its target. When a user clicks a user interface control, the control sends an action message to the target object as depicted in Figure 6.23.

Target/action

Figure 6-23. Target/action

The target/action relationship is typically defined using Interface Builder, in which you select a target object for a control, along with the specific action message that will be sent to the target. Target/action relationships can also be set (or modified) while an application is running.

Target/Action Example

The following steps take you through the process of building a simple example application that uses the target/action pattern. In this example, clicking a button in the main window causes the date and time to be updated in a text field.

Create the project and user interface

Since the first example provided detailed instructions on creating projects using Project Builder, this example generally does not repeat those instructions. If you forget how to do a step, refer back to the Nib Files project in Section 6.2 to refresh your recollection.

  1. Start Project Builder, if it is not already running.

  2. Create a new Cocoa application project called TargetAction.

  3. Open the main nib file.

  4. Drag a text field onto the main window and make it about four times the default width.

  5. Drag a button onto the main window

  6. Double-click the button and rename the button by typing Refresh.

When you’re finished, you should have a window that looks something like Figure 6.24.

Target/action application interface

Figure 6-24. Target/action application interface

Create the controller class

Here you’ll subclass NSObject to create a controller class for the application.

  1. Click the Classes tab of the MainMenu.nib window.

  2. Select NSObject from the list of classes.

  3. Press Return to create a new subclass and type MyController to replace the text MyObject.

Define the outlets of the class

The controller class needs an outlet for the text field so it can send messages to the text field.

  1. Select MyController in the Classes pane.

  2. Click the electrical outlet icon to the right of the class.

  3. Press Return to create a new outlet.

  4. Name this outlet textField and press Return.

Define an action for the class

You will define one action method for MyController called refresh:. When the user clicks the Refresh button, a refresh: message is sent to the target object, an instance of MyController. Action refers both to a message sent to an object when the user manipulates a control object and to the method that is invoked.

  1. Click Actions under MyConverter in the Classes pane shown in Figure 6.25.

    Defining an action

    Figure 6-25. Defining an action

  2. Press Return to create a new action, and type the name of the method, refresh. IB adds the : for you.

Generate an instance of the class

Now you must create a proxy instance of the controller class so you can make connections between the controller object and UI objects.

  1. Click MyController in the Classes list to select it.

  2. Choose Instantiate from the Classes menu. The instance will appear in the Instances pane.

Connect the interface controls to the class’s actions

For MyController to receive an action message from the button in the user interface, you must connect the button to MyController. The button object keeps a reference to its target using an outlet—not surprisingly, the outlet is named target.

You can view (and complete) target/action connections in the Connections Info window in Interface Builder. The upper-right column of the Connections Info window lists all action methods defined by the class of the target object and known by Interface Builder.

  1. Control-drag a connection from the Refresh button to the MyController instance in the MainMenu.nib window. When the instance is outlined, release the mouse button, as shown in Figure 6.26.

    Connecting a button to an object instance

    Figure 6-26. Connecting a button to an object instance

  2. In the Connections pane, make sure target in the Outlets column is selected.

  3. Select refresh: in the column on the right, as shown in Figure 6.27.

    Connecting the target to its action

    Figure 6-27. Connecting the target to its action

  4. Click the Connect button.

  5. Save the main nib file.

Developers new to Cocoa sometimes get confused when making action and outlet connections in Interface Builder. In general, you need only follow a simple rule to know which way to draw a connection line. Draw the connection in the direction that messages will flow:

  • To make an action connection, draw a line from a control object in the user interface, such as a button or a text field, to the custom instance that should receive the action message.

  • To make an outlet connection, draw a line from the custom instance to another object in the application.

The difference between target/action and outlet connections is depicted in Figure 6.28.

Target/action and outlet connections

Figure 6-28. Target/action and outlet connections

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.

Connect the custom class to the interface

So that you can tell when the refresh: action method is invoked, the application must provide some kind of feedback. For this example, clicking the Refresh button will print the current date and time.

Connect MyController to the text field in the main window as you did in Section 6.2. The refresh: method will use this outlet to send text to the text field.

  1. In the Instances pane of the nib file window, Control-drag a connection line from the MyController instance to the text field. When the instance is outlined, release the mouse button.

  2. Interface Builder brings up the Connections pane of the Info window. Select the outlet textField.

  3. Click the Connect button.

Generate the source files

At this point you’ve finished declaring MyController’s outlets, actions, and connections, so it’s time to create the source files for the class and add them to the Project Builder project.

  1. Go to the Classes pane of the nib file window.

  2. Select the MyController class.

  3. Choose Create Files in the Classes menu.

  4. Verify that the checkboxes in the Create column next to the .h and .m files are selected.

  5. Verify that the checkbox next to the TargetAction target is selected.

  6. Click the Choose button.

  7. Save the nib file.

Now we leave Interface Builder for this application. You’ll complete the application using Project Builder.

Implement MyController’s action method

Fill in the implementation of MyController’s refresh: method.

  1. In Project Builder, move MyController.h and MyController.m into the Classes group.

  2. Select MyController.m and insert the code for refresh: as shown:

    @implementation MyController
    - (IBAction)refresh:(id)sender
    {
         [textField setObjectValue:[NSCalendarDate date]];
    }
    @end

The method simply sends the current date and time to the text field so it is updated every time you click the Refresh button.

Build and debug the application

To smooth the task of debugging, Project Builder puts a graphical user interface over the GNU debugger, GDB. To help familiarize you with the debugger, use it now to set a breakpoint on the refresh: method.

  1. Click MyController.m in Project Builder’s Groups & Files list.

  2. Locate the refresh: method in the Text pane.

  3. Set a breakpoint by clicking in the column to the left of the code listing. See Figure 6.29.

    Setting a breakpoint

    Figure 6-29. Setting a breakpoint

  4. Click on the Build button to build the project.

  5. Run the debugger by clicking the Debug button, or press Command-R.

  6. When the application launches, click the Refresh button in its window.

  7. When you click Refresh, the debugger will activate, stop at the breakpoint, and allow you to examine the call stack and the value of local variables.

To perform complex debugging tasks, you can use the GDB console. Click the Console tab at the top righthand corner of the window to expose the Console. When you click in the Console window and press Return, the (gdb) prompt appears. There are many GDB commands that you can type at this prompt that are not represented in the user interface. For online information on these commands, enter help at the prompt.

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

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