Challenge: "Make a Delegate" + Book Erratum?


#1

I have a few questions at the very bottom of this post (if you would like to scroll through the long-winded verbosity below), but, mainly, I just thought I’d share my solution for the first challenge (“Make a Delegate”).

Initially, I implemented a naive, functional, but behaviorally clunky solution:

- (NSSize)windowWillResize: (NSWindow *)sender toSize: (NSSize)frameSize { frameSize.width = 2 * frameSize.height; return frameSize; }
After which, I implemented a more elegant behavior, similar to that of QuickTime X’s window behavior for maintaining aspect ratio:

- (NSSize)windowWillResize: (NSWindow *)sender toSize: (NSSize)frameSize { if ((frameSize.width / frameSize.height) > 2) frameSize.width = 2 * frameSize.height; else frameSize.height = 0.5 * frameSize.width; return frameSize; }
Of further note, I noticed that when you have a window selected in Interface Builder, it shows the Content Frame size under the Inspector (which is more useful than the window frame size, so that’s fine), but if you want to start off with a window 2:1 window initially, you need to account for the title bar, which under Snow Leopard (10.6.3) is 22 pixels. So, in Interface Builder, I set my Content Frame to be 200 pixels high by 78 pixels wide to start off at an initial window frame size of 200 by 100 pixels. Though, this doesn’t account for the title bar height to change, but it is a simple solution.

I was playing around a bit more and made a version without using delegate methods, but rather an Outlet to the window and using awakeFromNib to initially set the window’s aspect ratio:[code]@interface AppController : NSObject {
IBOutlet NSWindow *window;
}
@end

@implementation AppController

  • (void)awakeFromNib
    {
    NSSize aspectRatio;
    aspectRatio.width = 2.0;
    aspectRatio.height = 1.0;
    [window setAspectRatio: aspectRatio];
    }
    @end[/code]
    Would the immediate aforementioned implementation be considered the simplest way to do this? Is there a more elegant way of doing this? Is awakeFromNib the best place to do that?

[color=#800000]Also, there seems to be a slight erratum in the book. (Well, more of a clarification/wording issue, not technically an erratum.):[/color]

The book states (page 109):[quote]You can set the initial size of the window in the Size Inspector in Interface Builder.[/quote]
While this is technically true, it is misleading. The initial size of the window is set by virtue of setting the size of the Content Frame. A more accurate description would be as follows:[quote]You can set the initial size of the window’s content frame in the Size Inspector in Interface Builder. To properly set the initial window frame size to be of a 2:1 aspect ratio, you must account for the height of the title bar in addition to the content frame height.[/quote]