8.3. Subclassing Controls in Interface Builder

Occasionally you need to create your own custom controls to provide the right user experience within your application. Although you can create new controls from scratch by creating a custom NSControl subclass, you can often start from an existing control and just add your custom behavior. After all, if you need a special control that acts like a slider, you could save a lot of time and trouble by re-using the implementation in the NSSlider class.

Once you have defined a custom control, you will want to use that control in Interface Builder. This is simply a matter of declaring the control's class in your nib file's Classes tab, and changing a control instance to use your custom class. You have already seen examples of both these techniques: you have already created custom NSObject, NSDocument, and NSWindowController objects, and you have changed the class of a nib's File's Owner instance. You simply need to apply these techniques to other controls in your interface.

In the following Try It Out, you create a custom image view class that provides some custom resize logic for that view. This image view is meant to resize itself freely when its window changes size, as long as the image view is never smaller than its contents. This custom class simply inherits the rest of its behavior from the existing NSImageView class.

8.3.1.

8.3.1.1. Try It Out: Creating a Custom NSImageView Subclass
  1. Open the ImageWindow nib file in Interface Builder. ...

Get Beginning Mac OS® X Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.