When the user creates a new document in my NSDocument-based Cocoa application, I want the new document window to show a sheet where the user can set some initial document parameters.
This sheet shall not be displayed, however, when an existing document is loaded via File > Open.
Is there any existing mechanism which I can hook into for implementing this? Or any recommended way to do it?
Jay correctly stated in his answer that, in order to determine whether an instance of the
NSDocumentsubclass represents a new document rather than a loaded from a file one,initWithType:error:has to be overridden, which only gets called for new documents. But starting the sheet in there won’t work, unfortunately, as the window has not yet been created at the pointinitWithType:error:is called.The missing link to get this working is to instead set an instance variable named e.g.
newDocumenttoYESininitWithType:error:. The actual call to[NSApp beginSheet:…], then, has to be made inwindowDidBecomeKey:. Also, thenewDocumentvariable should be set toNOthere, in order to prevent the sheet from reappearing each time the window becomes key again.