• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSDocument with fixed/auto-generated filename
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDocument with fixed/auto-generated filename


  • Subject: Re: NSDocument with fixed/auto-generated filename
  • From: Erik Buck <email@hidden>
  • Date: Thu, 22 Jun 2006 18:28:13 -0400

In order to have your documents save without ever prompting the user for a path via a sheet, just implement the following method in your document class:

- (void)canCloseDocumentWithDelegate:(id)delegate shouldCloseSelector: (SEL)shouldCloseSelector contextInfo:(void *)contextInfo
{
[self saveToURL:[self fileURL] ofType:[self fileType] forSaveOperation:NSSaveOperation error:NULL];
[delegate document:self shouldClose:YES contextInfo:contextInfo];
}


Make sure that [self fileURL] and [self fileType] return a file URL to the magic save location and the correct file type string respectively.

Just delete the Save and Save As menu items from you MainMenu.nib


If you want to perform special document validation at close, - canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo: is a good place to do it. If the document can not be validated consider displaying a sheet explaining the problem and then specify NO as the shouldClose: argument.


You only have to implement ONE well documented NSDocument method to do what you are _now_ requesting.

And yes, I just whipped up a little demo application to verify this works.


See

closeAllDocumentsWithDelegate:didCloseAllSelector:contextInfo:
Iterates through all the open documents and tries to close them one by one using the specified delegate.


- (void)closeAllDocumentsWithDelegate:(id)delegate didCloseAllSelector:(SEL)didCloseAllSelector contextInfo:(void *) contextInfo

Discussion

Each NSDocument object is sent canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo:, which, if the document is dirty, gives it a chance to refuse to close or to save itself first. This method may ask whether to save or to perform a save.

The didCloseAllSelector callback method is invoked with YES if all documents are closed, and NO otherwise. Pass the contextInfo object with the callback. The didCloseAllSelector callback method should have the following signature:

- (void)documentController:(NSDocumentController *)docController didCloseAll: (BOOL)didCloseAll contextInfo:(void *)contextInfo

Availability

Available in Mac OS X v10.0 and later.

and

canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo:
If the receiver is not dirty, this method immediately calls the shouldCloseSelector callback on the specified delegate with YES.


- (void)canCloseDocumentWithDelegate:(id)delegate shouldCloseSelector: (SEL)shouldCloseSelector contextInfo:(void *)contextInfo

Discussion

If the receiver is dirty, an alert will be presented giving the user a chance to save, not save, or cancel. If the user chooses to save, this method will save the document. If the save completes successfully, this method will call the callback with YES. If the save is canceled or otherwise unsuccessful, this method will call the callback with NO. This method may be called by shouldCloseWindowController:. It is also called by NSDocumentController’s closeAllDocuments. You should call it before you call close if you are closing the document and want to give the user a chance to save any edits. Pass the contextInfo object with the callback.

The shouldCloseSelector callback method should have the following signature:

- (void)document:(NSDocument *)doc shouldClose:(BOOL)shouldClose contextInfo:(void *)contextInfo

Availability

Available in Mac OS X v10.0 and later.



_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Re: NSDocument with fixed/auto-generated filename (From: Erik Buck <email@hidden>)
 >Re: NSDocument with fixed/auto-generated filename (From: Martin Redington <email@hidden>)

  • Prev by Date: Re: How to force an initial column sort with NSTableView and controller?
  • Next by Date: (no subject)
  • Previous by thread: Re: NSDocument with fixed/auto-generated filename
  • Next by thread: NSMatrix setCellClass: vs setPrototype:
  • Index(es):
    • Date
    • Thread