How do I dismiss an NSPanel when creating or opening a new document?

Posted by mipadi on Stack Overflow See other posts from Stack Overflow or by mipadi
Published on 2010-05-13T14:24:44Z Indexed on 2010/05/14 9:34 UTC
Read the original article Hit count: 312

I am working on a document-based Cocoa application. At startup, the user is presented with a "welcome panel" (of type NSPanel) with buttons for common actions like "Create New Document" and "Open Existing Document". These actions are linked to the first responder's newDocument: and openDocument: actions, respectively, just like the matching items in the File menu.

Everything works as expected...with two caveats:

  1. The welcome panel is not dismissed when creating or opening a new document.
  2. Document windows do not have focus when they are created.

I have partially solved #1 by making my application controller a delegate of the welcome panel. When clicking the "Open Existing Document" button, the panel resigns its key status (since a file browser dialog is being opened), so I can close the panel in the delegate's windowDidResignKey: method. However, I can't figure out how to close the panel when creating a new document, since I can't find a notification that is posted, or a delegate method that is called, when creating a new document. And ultimately, #2 is still a problem, since the document windows don't gain focus when they're created.

I have only subclassed NSDocument -- I'm not using a custom document or window controller at all. I've also tried changing the panel to an NSWindow, thinking that an NSWindow may behave differently, but the same problems are occurring.

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about nsdocument