Notifications in Cocoa

Notifications in Cocoa

Notifications seem unnecessarily confusing the way they are documented.

I want to know when my my window resizes so I notice in the documentation for NSWindow that there is a NSWindowDidResizeNotification.  Great seems that’s what I want to write a handler for.

What do I do?  The documentation does not describe what the handler function signature should be but I locate some sample code that uses:

– (void)windowDidResize:(NSNotification*)theNotification

so I add that to my NSWindow derived class for my main window but the method is not called.

I read up on Notifications and they talk about registering for notifications you are interested in but that seems unnecessary for this particular event, especially since I’ve subclassed the window.  What is it in my background that expects that by simply subclassing the window that, that would be sufficient to capture the resize event?  Is it my Windows background?

Anyway, I added

    [self setDelegate:self]; // Set as delegate in order to receive notifications

in my MainWIndow awakeFromNib handler and my resize handler started getting called.

In another application I had to set the delegate again when adding the resize handler to the NSHandler object for a Window wasn’t sufficient.

"Delegates and Data Sources" in the Cocoa documentation sheds some light on this.

Leave a Reply