Re: Single document behavior
Re: Single document behavior
- Subject: Re: Single document behavior
- From: Brian Webster <email@hidden>
- Date: Tue, 26 Nov 2002 22:19:04 -0600
On Tuesday, November 26, 2002, at 06:27 PM,
email@hidden wrote:
I've got an NSDocument based app, and I want to slightly alter the
behavior
such that a maximum of one document will be present at any given time.
That is, if a document exists and the user chooses "New" from the file
menu,
or the app gets a reopen AppleEvent, I want to prompt the user to save
the
current document if it's dirty, cancel the open, etc before continuing
through with the new document process.
What is the cleanest way to do this with AppKit?
Well, I'm sure there are several different valid ways to do this, but
I'll tell you the way I'd probably do it. NSDocumentController is a
singleton class that controls the creation and tracking of NSDocuments.
You can subclass this object and override a couple of methods that
will be called whenever a document is going to be created.
The ones you're probably going to be interested in are
openDocumentWithContentsOfFile:display: and
openUntitledDocumentOfType:display:, which get called in response to
the Open and New commands, respectively. You can access the current
document through the currentDocument method and take appropriate action
if a document is already open. It will be a little tricky, since the
non-modal nature of the sheets that come up will require saving some
state and coming back to it once the user responds, but very doable.
So, you'll want to make a subclass of NSDocumentController and make it
the shared singleton instance that is used. Instructions on how to do
this are in the Cocoa programming topics in the docs, under Program
Design->Document Based Applications, look for the section "Creating a
Subclass of NSDocumentController".
--
Brian Webster
email@hidden
http://homepage.mac.com/bwebster
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.