cocoansfontpanelnscolorpanel

NSFontPanel - How to handle closing


I need to handle that NSFontPanel has been closed. Is there any method which is called, when that happens? Thx for reply.


Solution

  • NSFontPanel is a subclass of NSPanel, which is a subclass of NSWindow. NSWindow has many delegate methods that will inform you of changes in window state.

    In your window controller or app delegate, declare conformance to NSWindowDelegate, then obtain the font panel and set its delegate to the controller object. Finally, implement -windowWillClose: in the controller object, and take whatever action you need there.

    For example:

    /* AppDelegate.h */
    @interface AppDelegate : NSObject <NSWindowDelegate>
    @property (assign) IBOutlet NSWindow *window;
    @end
    
    /* AppDelegate.m */
    @implementation AppDelegate
    
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
      NSFontPanel *fp = [[NSFontManager sharedFontManager] fontPanel:YES];
      fp.delegate = self;
    }
    
    - (void)windowWillClose:(NSNotification *)notification
    {
      if(notification.object == [[NSFontManager sharedFontManager] fontPanel:NO])
      {
        /* Handle font panel close here */
        NSLog(@"Font panel closing");
      }
    }
    
    @end