Developing Java Applications: A Tutorial | Building a Simple Application

Connecting Objects

Interface Builder enables you to connect a custom object to its outlets and to the objects in the user interface that invoke action methods of the custom object. This connection information is stored in the nib file along with the user interface objects, class definitions, and nib resources.

Create an Instance of the Controller Class

Before you can connect a custom object to objects in the user interface, you must create an instance of the object. (This is not a real instance, but a "proxy" instance representing the connections to the object. The real instance is created when the nib file is loaded.)

  1. Select the TempController class in the Classes display of the nib file window.
  2. Choose Instantiate from the Classes menu.

The nib file window automatically changes to the Instances display, and an instance of the TempController class (depicted as a cube) appears in the display.

Connect the Controller to Its Outlets

Follow this Interface Builder procedure to connect the TempController custom object to its outlets:

  1. Control-drag from the cube representing the custom object to the Fahrenheit text field (the editable field, not the label). A thick black line follows the cursor while you drag.

    "Control-drag" means to hold down the Control key while dragging the mouse (moving it with the mouse button pressed).



  2. When a box encloses the Fahrenheit field, release the mouse button.

    Interface Builder shows the Connections display of its Inspector. The left column of this display lists the outlets defined by TempController.



  3. Select the fahrenheit outlet.
  4. Click the Connect button.

Repeat steps 1 through 4 for the celsius outlet.

Connect the Action of the Controller

Follow this Interface Builder procedure to connect the action method defined by TempController to the objects that might invoke that method:

  1. Control-drag from the the Fahrenheit field (the editable field, not the label) to the cube representing the custom object. A thick black line follows the cursor while you drag.




  2. When a box encloses the Fahrenheit field, release the mouse button.

    Interface Builder shows the Connections display of its Inspector. The left column of this display lists the outlets defined by TempController.



  3. Select the convert() action.

    You might first have to click the target item under Outlets to get to the action.

  4. Click the Connect button.

Repeat steps 1 through 4 for the Celsius field.

Connect the Responders

As a convenience to users, you want the insertion point to be in a certain field after the application is launched. For the same reason (convenience), you want users to be able to switch between the fields without having to use the mouse--they should be able to tab between the fields. You can specify this behavior entirely in Interface Builder:

  1. Click the Instances tab of the nib file window.
  2. Control-drag a connection line from the window icon to the Fahrenheit field.




  3. In the Connections display of the inspector, select initialFirstResponder .
  4. Click the Connect button of the inspector.
  5. Control-drag a connection line from the Fahrenheit field to the Celsius field.
  6. In the Connections display, select nextKeyView and click Connect.
  7. Control-drag a connection line from the Celsius field to the Fahrenheit field.
  8. In the Connections display, select nextKeyView and click Connect.

What have you just done? You've specified the sequence of responder objects in the user interface that are to receive the focus of keyboard events when users press the Tab key.

Related Concepts

Test the User Interface

You can now test the user interface you've constructed with Interface Builder. Save the nib file and choose Test Interface from the Document menu. Interface Builder goes into test mode and the window and text fields you've just created behave as they would in the final application--except, of course, there is yet no custom behavior.

Notice that the insertion point is initially in the Celsius field. Press the Tab key; note how the insertion point jumps between the fields. Type something into one of the fields, then select it and choose Cut from the Edit menu. Click in the other text field and choose Paste from the Edit menu. These are but a couple of examples of features you get in any application with little or no work on your part.


Previous | Next
© 1998 Apple Computer, Inc.