Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

satyam90

macrumors regular
Original poster
Jul 30, 2007
242
0
Bangalore, India
I am using Cocoa with Obj C
I am having a window and implemented windowWillClose:(NSNotification *)aNotification which works fine only when I click "x" on the top left of the window.
But when I use the keyboard Command+W, the window is closed, but this is not calling windowWillClose. why? How to handle it?
 

idelovski

macrumors regular
Sep 11, 2008
235
0
I think you should implement delegate windowShouldClose: like this

- (BOOL)windowShouldClose:(NSWindow *)sender

Strange that you say that your Notification works!? How? Isn't notification there just to tell you something already happened.
 

satyam90

macrumors regular
Original poster
Jul 30, 2007
242
0
Bangalore, India
There is a delegate method available in NSWindow, windowWillClose : (NSNotification *) notification
Please see the NSWindow reference.
 

idelovski

macrumors regular
Sep 11, 2008
235
0
Ah!

The point is you don't want to prevent it - you just need information about it and you're quite happy with it? Does that mean "notification works fine?"
 

idelovski

macrumors regular
Sep 11, 2008
235
0
This worked for me:

Code:
- (id)init
{
   NSNotificationCenter  *nc = [NSNotificationCenter defaultCenter];
   
   [nc addObserver:self
          selector:@selector(windowWillClose:)
              name:NSWindowWillCloseNotification
            object:nil];  // pass window to observe only that window
            
   return (self);
}

- (void)windowWillClose:(NSNotification *)notification
{
   NSLog (@"Notification %@", [notification name]);
}

When I run this code, method windowWillClose: is notified on mouse click in close box, on Command-W and on Command-Q.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.