Beginning Mac OS X Snow Leopard Programming by Michael Trent & Drew McCormack

Beginning Mac OS X Snow Leopard Programming by Michael Trent & Drew McCormack

Author:Michael Trent & Drew McCormack
Language: eng
Format: epub
ISBN: 9780470577523
Publisher: Wiley


* * *

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.

This practice of starting from an existing control applies to Interface Builder as well. After you have decided to subclass a control from an existing class, drag the existing class into your interface from the Library and then change its Class in the Identity Inspector. You have already seen examples of both these techniques: you have dragged many controls out of the Library, and you have changed the class of a nib’s File’s Owner instance. You simply need to apply these techniques to other controls to modify their behavior through subclassing.

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.

TRY IT OUT Creating a Custom NSImageView Subclass

1. In Xcode, create a new Objective-C object subclassed from NSObject named SlideImageView.m. Remember to create the corresponding header file.

2. In SlideImageView.h, change the superclass for SlideImageView from NSObject to NSImageView:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.