• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Chapter 7. Changing Interface Elements > Changing Object Classes

7.10. Changing Object Classes

Just because an interface object belongs to a certain class doesn't necessarily mean that it has to remain that way. In the following example, you'll discover that it's fairly simple to change classes to add extra capabilities to an application. Changing classes may create new attributes (as it does in this example) or offer new Outlets and Actions. Use the Custom Class pane to look for possible options; you may find a better class to suit your needs.

  1. In Interface Builder, select the Term instance in the NIB window. Make sure the associated Terminal design window is visible.

  2. In the Info window, open the Attributes pane (-1). The titlebar of the Info window says "NSWindow Info," indicating the Term instance's type. Notice that the Utility Window feature is grayed out; it is not available to the NSWindow class.

  3. Open the Custom Class pane (-5). This pane lets you choose from classes that are closely related to the selected item. Notice that NSWindow is the selected class (it's highlighted in gray).

  4. Double-click NSPanel. When you do, the NIB window updates to show the NSPanel class, and the Info window shows the class's attributes.

    NIB Mining Strategies

    Just as a body needs its heart, kidneys, and brain, the objects in nib files often have indispensable connections to other objects that provide vital functions. In researching the previous example, it took several attempts until I discovered which objects were vital (the ColorInspector and the InspectorController) and which ones were not (the EmulationInspector and the ProcessesInspector, among others).

    When mining for functions, start by narrowing down which object instance does the job you want to copy. Select that instance and use the Attributes pane to look at the Outlets (instance variables) connected to that object. Some of the connections will be to easily replaceable interface items, such as sliders and color wells, while others will be to more application-intensive objects, such as the InspectorController. The word "Controller" is a big tip-off that an object contains code that provides the power behind an interface.

    Copy the object instance to your new NIB file, create some appropriate controls, and start by making the most obvious connections to and from those controls. As a rule, you don't have to rebuild every single control that appeared in the original NIB file. The controls depend on the object, but the object doesn't necessarily depend on the controls. To put it another way: the controls tend to message the new object rather than vice versa.

    After adding some interface controls, start testing them. If everything works, you're golden. If not, you may have to copy over other objects and test the interface again. Look for controllers and other algorithm-sounding items such as managers. Take note of the name of each object Outlet as well as the object it connects to. Copy one object at a time and connect it to the primary instance until things begin to work. When building the previous example, it took several iterations until things began to function properly. Until they did, the Terminal windows I opened to test the changes tended to die in spectacular ways.

    The example you just worked through takes advantage of the fact that functionality already existed within the application. Extracting that function from one kind of window and placing it in another, while not simple, is much more likely to succeed than trying to borrow something similar from another application.

    Figure 7-41. The Terminal design window has a different look when designated as a Utility window.

  5. In the NIB window, click on the Instances tab. Re-select the Term instance. Notice how the Info window's titlebar now reads "NSPanel Info." In the Attributes pane (-1), the two formerly grayed-out features—"Utility window" and "Non activating Panel"—are now available.

  6. Place a checkmark in the box next to "Utility window"; the look of the Terminal design window updates, as shown in Figure 7-41.

  7. Save your changes (File → Save, -S).

  8. Switch back to the Terminal, open a new window (-N), and check out the changes. Take note that the color and transparency sliders will not work with the NSPanel window (pressing on the Clear Scrollback button works as expected).

  9. To restore the previous class settings, return to Interface Builder, select the Term instance, and open the Attributes pane (-1).

  10. Uncheck "Utility window" in the Attributes pane.

  11. Open the Custom Class pane (-4). Double-click NSWindow; the Term instance reverts back to the NSWindow class.

  12. Choose File → Save (-S) and save your changes.

  13. Return to the Terminal application and ensure that the color and transparency slider functions have been restored.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint