Handling Command+W for a window in window controller

Discussion in 'Mac Programming' started by satyam90, Oct 17, 2008.

  1. satyam90 macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #1
    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?
     
  2. idelovski macrumors regular

    Joined:
    Sep 11, 2008
    #2
    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.
     
  3. satyam90 thread starter macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #3
    There is a delegate method available in NSWindow, windowWillClose : (NSNotification *) notification
    Please see the NSWindow reference.
     
  4. idelovski macrumors regular

    Joined:
    Sep 11, 2008
    #4
    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?"
     
  5. idelovski macrumors regular

    Joined:
    Sep 11, 2008
    #5
    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.
     

Share This Page