Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Apple > Operating Systems

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Replacing the Interface Outlet with a Cocoa Binding

Replacing the Interface Outlet with a Cocoa Binding

At this point, the example utilizes an outlet to integrate the project's code with the text field in the main interface window. But there's an alternative method for retrieving the text field's value. This project could make use of a Cocoa binding, which is a method of connecting an attribute of a user interface element to the code, rather than connecting the interface element itself. The use of bindings can help reduce the amount of code in your project.

To utilize a binding in this project, we must first eliminate the textField outlet. In the Project Navigator, click the MainMenu.xib file again. In the main interface window, select the text field. In the Connections inspector, under Referencing Outlets, click the "x" button on the link between "textField" and "App Delegate" to sever the outlet link (see Figure 20).

Figure 20 Severing the link between the textField outlet and the App Delegate.

Next, display the Bindings inspector (Control-Option-Command-7). Under Value, from the Bind To pop-up menu, select App Delegate; then enter theName into the Model Key Path field. Select the Continuously Updates Value checkbox, and leave all other checkboxes set to their default values (see Figure 21).

Figure 21 Configuring a binding for the text field.

In the Project Navigator, click the AppDelegate.applescript file, and then modify the code as follows:

property theName : ""

on buttonClicked_(sender)
      display alert "Hello there " & theName
end buttonClicked_

Figure 22 shows the completed code.

Figure 22 The AppDelegate.applescript file, modified to use a binding.

If you run the project again (Command-R), the result is the same, and the text field's value is immediately passed to the code of the project.

  • + Share This
  • 🔖 Save To Your Account