Re: Newbie: Getting an NSWindow and setting its delegate
Re: Newbie: Getting an NSWindow and setting its delegate
- Subject: Re: Newbie: Getting an NSWindow and setting its delegate
- From: Martin Linklater <email@hidden>
- Date: Sat, 30 Jul 2005 16:31:39 +0100
Thanks for all the great help folks.
CTRL-dragging to the window instance icon rather than the window
itself was the key. It's working a charm now.
- Martin
On 30 Jul 2005, at 16:28, Michael Ströck wrote:
Hi,
You are probably thinking way to complicated ;-) I'm just going
through the book myself, so as a help for you and a reminder for
me, here's how I did it. Of course, I'm not a Cocoa pro, but I
think it is a correct way to do it.
DO NOT read any further if you want to figure it out yourself, as
this is a complete walk-through.
1. Create Project
2. Open standard NIB in IB
3. Subclass NSObject and name your class "AppController" or
whatever you want
4. Add an outlet to "AppController" named "myWindow" of type NSWindow
5. Instantiate "AppController"
6. CTRL-drag from "Window" to "AppController"; connect "delegate"
7. CTRL-drag from "AppController" to "Window"; connect "myWindow"
8. Type in the source below
9. You're done
Here's the entire AppController.h:
=========================
#import <Cocoa/Cocoa.h>
@interface AppController : NSObject
{
IBOutlet NSWindow *myWindow;
}
- (NSSize)windowWillResize:(NSWindow *)sender
toSize:(NSSize)proposedFrameSize;
@end
=========================
and the entire AppController.m
=========================
#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);
return proposedFrameSize;
}
- (void) dealloc
{
[myWindow release];
[super dealloc];
}
@end
=========================
Am 30.07.2005 um 16:05 schrieb Martin Linklater:
Apologies for a real Newbie question.
I'm going through Aaron Hillegass's book 'Cocoa Programming for
Mac OSX' and I'm stuck on the challenge at the end of chapter 5.
The challenge is this:
'Create a new application with one window. Make an object that is
a delegate of the Window. As the user resizes the window, make
sure that the window always remains twice as tall as it is wide.'
My problem is that I don't quite know how to approach the problem.
I've tried two approaches so far, both of which seem to fail:
1) Try to set the delegate of the default Window which is created
when I create the project. Problem is, IB doesn't let me Control-
drag from the window to my AppController class. I tried to set the
windows delegete in code, but although I can see the 'setDelegate'
method for NSWindow in the docs I don't know how to actually get a
pointer to the window to begin with... How do you 'find' a pointer
to the default NSWindow object that is created with the project ?
2) Subclass the NSWindow object and use my own window. I've
deleted the instance of the standard window from IB, but IB isn't
letting me instantiate my own class... It lets me create a
subclass of NSWindow and create the source files for it, but the
'Instantiate this class' menu option is greyed out. Why ? I've
tried to create an instance of my subclass in the AppController
init method (using [[MyWindow alloc] init] ), but that doesn't
seem to create anything, and I can't tell why. It's not throwing
errors or anything, but it's not creating a window either. I'm
puzzled.
Can anyone point me in the right direction of how I complete this
challenge ? I'm not after an in-depth explanation or anything -
just a pointer to some docs or a few hints to get my brain working
properly.
Thanks for any help you can give.
- Martin
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40stroeck.com
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden