Online Book Reader

Home Category

Learn Objective-C on the Mac - Mark Dalrymple [18]

By Root 923 0
■ Actions are methods we write that can be executed directly as a result of user interaction, such as at the click of a button.

■ Outlets are pointers to objects in our nib file. Outlets allow us to access objects in the nib from our code.

Actions and outlets are typically contained in your controller classes (although they are sometimes used elsewhere).

Declaring Outlets


Outlets are Objective-C instance variables that are declared using a special keyword: IBOutlet. An outlet is really nothing more than an object pointer that can be linked to an object in your user interface. So, for example, your controller class might declare an outlet to an editable text field like this:

In this example, as far as your code is concerned, nameField is a pointer to whatever text field you link it to in Interface Builder. It will behave exactly the same way as a pointer to an object that you allocated and initialized yourself. Once an outlet is linked to an object, you can retrieve or set its value, hide it, disable it, or do anything else that the object supports. We’ll see how to make the link between an outlet and an object in a nib file in a moment.

OUTLET CHANGES

Prior to Objective-C 2.0, the IBOutlet keyword was placed in the instance variable declaration, like this:

IBOutlet NSTextField *nameField

Since the release of Objective-C 2.0, Apple’s sample code has been moving toward placing the IBOutlet keyword in the property declaration, like this:

Both mechanisms are still supported and, for the most part, there is no difference in the way things work based on where you put the keyword. There is one exception to that, however: if you declare a property with a different name than its underlying instance variable (which can be done in the @synthesize directive), then you have to put the IBOutlet keyword in the property declaration, not in the instance variable declaration, in order for it to work correctly. Although both work, we’re going to follow Apple’s lead and put the IBOutlet keyword on the property declaration, for the most part. In some of our applications, we’ll have outlets to objects without any matching properties, and in those cases we’ll put the IBOutlet keyword in front of the instance variable declaration.

You can read more about the new Objective-C properties in the second edition of Learn Objective-C on the Mac, by Mark Dalrymple and Scott Knaster (Apress, 2008), and in The Objective-C 2.0 Programming Language available from Apple’s developer site at: http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/ObjC.pdf

Declaring Actions


Actions are Objective-C methods that can be invoked directly from your application’s user interface. They are methods just like any other Objective-C methods you’ve written, but they get executed when a user interface item is utilized. If you link a button to an action method, for example, the code in your action method will fire any time that button is clicked. If you link a text field to an action, its action method will fire any time the user tabs out of that text field or otherwise moves to another control. Exactly what will cause the method to fire depends on the type of object that is linked to it and, sometimes, how the attributes of that object are set. A slider, for example, may cause your action method to fire once after the user releases the mouse button, or it may repeatedly cause your method to fire as the slider is used, depending on how you set up the slider instance in Interface Builder.

Actions are created exactly the way other Objective-C methods are, except that they must be declared using a special return type: IBAction. Actions must take a single argument (typically declared as type id). This argument is used to tell the method which interface item is calling it. Action method declarations have to look like this:

The name of the method can be anything you want, but the return type has to be IBAction, and the method has to take one argument of type id, which will be a pointer to the object that triggered the action.

Return Main Page Previous Page Next Page

®Online Book Reader