Exercice delegate of NSWINDOW


#1

Hi

I’ve done the exercice where you have to setup a delegate of an NSWINDOW.

My code works well, when the user change the size of the window I contraint it size to W = 2*H

But about setting the delegate of the NSWINDOW. I did this by draging delegate from the window to the app controler in Interface Builder. I didn’t set it in my code

So for exemple in the INIT method, I didn’t write something like this
theWindow = [[NSWINDOW alloc] init];
[theWindow setDelegate:self];

I’ve tried to not set the delegate in Interface Builder but in the code. But in this case it doesn’t work

I’m not at home so I don’t have my code. but if I remember what I’ve done was :

  • setup an IBOUTLET in the header say IBOutlet NSWindow *theWindow;
  • then in my INIT method did the code above

So is it ok to define the delegate’s Window using Interface builder only (and so no IBoutlet and no setDelegate in the init) ?
If we need to set it by programation, how we’ll do that (so with a setDelegate in the init) ?

Also. Anywhere we call the init method, so just because I’m curious, when the init method is called, and by who ?

Thanks
(sorry for my english, I hope you understand what i try to mean)


#2

[quote=“pascalf”]Hi

I’ve done the exercice where you have to setup a delegate of an NSWINDOW.

My code works well, when the user change the size of the window I contraint it size to W = 2*H

But about setting the delegate of the NSWINDOW. I did this by draging delegate from the window to the app controler in Interface Builder. I didn’t set it in my code
[/quote]

NSWINDOW?! Does this even compile? I always thought Cocoa is case-sensitive…

Are you sure you’re dragging from the “Window (Window)” in the doc window? See Page 17, “The Doc Window”

Also, you don’t need to [[alloc] init] NSWindows!

The IBOutlet *theWindow looks good (though I’d check for case sensitivity) and I’ve also added to AppController.h:

@interface AppController : NSObject <NSWindowDelegate> {

After that you have to make your AppController a delegate of your “theWindow”, like you wrote above

[quote=“pascalf”]
Also. Anywhere we call the init method, so just because I’m curious, when the init method is called, and by who ?

Thanks
(sorry for my english, I hope you understand what i try to mean)[/quote]

There was a long discussion about init over at Cocoa Community, see cocoacommunity.com/showthread.php?tid=21


#3

I’m getting this error:

my appController.m:

@implementation appController -(id)init { [super init]; theWindow = [[NSWindow alloc] init]; return self; } -(NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize { NSSize mySize; mySize.width = 200.0; mySize.height = 100.0; [theWindow setContentSize:mySize]; } @end

:confused: