Reputation: 1675
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?
Upvotes: 2
Views: 641
Reputation: 1675
Jay correctly stated in his answer that, in order to determine whether an instance of the NSDocument
subclass 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 point initWithType:error:
is called.
The missing link to get this working is to instead set an instance variable named e.g. newDocument
to YES
in initWithType:error:
. The actual call to [NSApp beginSheet:…]
, then, has to be made in windowDidBecomeKey:
. Also, the newDocument
variable should be set to NO
there, in order to prevent the sheet from reappearing each time the window becomes key again.
Upvotes: 3
Reputation: 6638
Override initWithType:error:
in your NSDocument
subclass.
From the documentation (see NSDocument Class Reference):
You can override this method to perform initialization that must be done when creating new documents but should not be done when opening existing documents.
Upvotes: 3