In article
<933bf1aa-5d71-464b-9d59-5a4f79d36439.TakeThisOut@k37g2000hsf.googlegroups.com>,
superavit <jasonkrac.TakeThisOut@gmail.com> wrote:
> Guys/Gals,
> I am trying to complete the challenge in Hillegas book, chapter 6. I
> need to create a delegate to a window that constraints its resinzing.
> I have the following code:
> // AppController.h
> // Delegate Try
> #import <Cocoa/Cocoa.h>
> @interface AppController : NSObject
> {
> IBOutlet NSWindow *myWindow;
> }
>
> - (NSSize)windowWillResize:(NSWindow *)sender
> toSize:(NSSize)proposedFrameSize;
> @end
> // AppController.m
> // Delegate Try
> #import "AppController.h"
> @implementation AppController
> - (id)init
> {
> [super init];
> myWindow = [[NSWindow alloc] init];
> [myWindow setDelegate:self];
> return self;
> }
> - (NSSize)windowWillResize:(NSWindow *)sender
> toSize:(NSSize)proposedFrameSize
> {
> proposedFrameSize.height = (proposedFrameSize.width*2);
> NSLog(@"myWindow will resize to %f wide and %f tall",
> proposedFrameSize.width, proposedFrameSize.height);
> return proposedFrameSize;
> }
> - (void) dealloc
> {
> [myWindow release];
> [super dealloc];
> }
> @end
> I connected the Appcontroller as a delegate of the window and the
> window as an outlet of the Appcontroller. But as the application
> starts and resizes, nothing happens along the lines of
> constraining...
> Help this newbie! Please
> Jason
Something you're not showing us is going wrong, because this works as
you intend here.
Some notes:
1. When the app launches, AppController>>init will be invoked. That will
blow away any value you've established for the myWindow outlet in the
nib. However the app controller will still be the delegate for the nib
window as long as you actually did establish that relationship. If
you're *not* setting the controller as the delegate of the window in the
nib, the code above isn't going to work as you expect. The window
on-screen is the one from the nib. The window you create in your init
method never gets shown. As it's presented, your init and dealloc
methods are not having a meaningful effect on the operation of your
program.
2. As noted previously, you really should get into the habit of caring
about the return value from the superclass' initializer. It doesn't
strictly matter here, but consistency is easier to debug and reinforces
the habit for when it does matter.
--
"Harry?" Ron's voice was a mere whisper. "Do you smell something ... burning?"
- Harry Potter and the Odor of the Phoenix