| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/AppKit.framework |
| Availability | Available in Mac OS X v10.0 and later. |
| Companion guide | |
| Declared in | NSDocumentController.h |
| Related sample code |
An NSDocumentController object manages an application’s documents. As the first-responder target of New and Open menu commands, it creates and opens documents and tracks them throughout a session of the application. When opening documents, an NSDocumentController runs and manages the modal Open panel. NSDocumentController objects also maintain and manage the mappings of document types, extensions, and NSDocument subclasses as specified in the CFBundleDocumentTypes property loaded from the information property list (Info.plist).
You can use various NSDocumentController methods to get a list of the current documents; get the current document (which is the document whose window is currently key); get documents based on a given filename or window; and find out about a document’s extension, type, display name, and document class.
In some situations, it is worthwhile to subclass NSDocumentController in non-NSDocument-based applications to get some of its features. For example, the NSDocumentController management of the Open Recent menu is useful in applications that don’t use subclasses of NSDocument.
– documentForURL:
– openUntitledDocumentAndDisplay:error:
– makeUntitledDocumentOfType:error:
– openDocumentWithContentsOfURL:display:error:
– makeDocumentWithContentsOfURL:ofType:error:
– reopenDocumentForURL:withContentsOfURL:error:
– makeDocumentForURL:withContentsOfURL:ofType:error:
– documents
– addDocument:
– currentDocument
– documentForWindow:
– hasEditedDocuments
– removeDocument:
– documentClassNames
– defaultType
– documentClassForType:
– displayNameForType:
– typeForContentsOfURL:error:
– fileExtensionsFromType: Deprecated in Mac OS X v10.5
– typeFromFileExtension: Deprecated in Mac OS X v10.5
– closeAllDocumentsWithDelegate:didCloseAllSelector:contextInfo:
– reviewUnsavedDocumentsWithAlertTitle:cancellable:delegate:didReviewAllSelector:contextInfo:
– maximumRecentDocumentCount
– clearRecentDocuments:
– noteNewRecentDocumentURL:
– noteNewRecentDocument:
– recentDocumentURLs
– presentError:
– presentError:modalForWindow:delegate:didPresentSelector:contextInfo:
– willPresentError:
– documentForFileName: Deprecated in Mac OS X v10.4
– fileNamesFromRunningOpenPanel Deprecated in Mac OS X v10.4
– makeDocumentWithContentsOfFile:ofType: Deprecated in Mac OS X v10.4
– makeDocumentWithContentsOfURL:ofType: Deprecated in Mac OS X v10.4
– makeUntitledDocumentOfType: Deprecated in Mac OS X v10.4
– openDocumentWithContentsOfFile:display: Deprecated in Mac OS X v10.4
– openDocumentWithContentsOfURL:display: Deprecated in Mac OS X v10.4
– openUntitledDocumentOfType:display: Deprecated in Mac OS X v10.4
– setShouldCreateUI: Deprecated in Mac OS X v10.4
– shouldCreateUI Deprecated in Mac OS X v10.4
Returns the shared NSDocumentController instance.
+ (id)sharedDocumentController
The shared NSDocumentController instance.
If an NSDocumentController instance doesn’t exist yet, it is created.
Initialization reads in the document types from the CFBundleDocumentTypes property list (in Info.plist), registers the instance for NSWorkspaceWillPowerOffNotifications, and turns on the flag indicating that document user interfaces should be visible. You should always obtain your application’s NSDocumentController using this method.
NSDocumentController.hAdds the given document to the list of open documents.
- (void)addDocument:(NSDocument *)document
The open... methods automatically call addDocument:. This method is mostly provided for subclasses that want to know when documents arrive.
NSDocumentController.hReturns the time interval in seconds for periodic autosaving.
- (NSTimeInterval)autosavingDelay
A value of 0 indicates that periodic autosaving should not be done at all. NSDocumentController uses this number as the amount of time to wait between detecting that a document has unautosaved changes and sending the document an autosaveDocumentWithDelegate:didAutosaveSelector:contextInfo: message. The default value is 0.
NSDocumentController.hEmpties the recent documents list for the application.
- (IBAction)clearRecentDocuments:(id)sender
This is the action for the Clear menu command, but it can be invoked directly if necessary.
NSDocumentController.hIterates 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
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 |
NSDocumentController.h
Returns the directory path to be used as the starting point in the Open panel.
- (NSString *)currentDirectory
The first valid directory from the following list is returned:
The directory location where the current document was last saved
The last directory visited in the Open panel
The user’s home directory
NSDocumentController.h
Returns the NSDocument object associated with the main window.
- (id)currentDocument
This method returns nil if it is called when its application is not active. This can occur during processing of a drag-and-drop operation, for example, in an implementation of readSelectionFromPasteboard:. In such a case, send the following message instead from an NSView subclass associated with the document:
[[[self window] windowController] document]; |
NSDocumentController.hReturns the name of the document type that should be used when creating new documents.
- (NSString *)defaultType
The default implementation of this method returns the first Editor type declared by the CFBundleDocumentTypes array in the application's Info.plist, or returns nil if no Editor type is declared. You can override it to customize the type of document that is created when, for instance, the user chooses New in the File menu.
NSDocumentController.h
Returns the descriptive name for the specified document type, which is used in the File Format pop-up menu of the Save As dialog.
- (NSString *)displayNameForType:(NSString *)documentTypeName
The name of a document type, specified by CFBundleTypeName in the application’s Info.plist file.
The descriptive name for the document type specified by documentTypeName. If there is no descriptive name, returns documentTypeName.
For a document-based application, supported document types are specified in the Info.plist file by the CFBundleDocumentTypes array. Each document type is specified by a dictionary in this array, and is named by the CFBundleTypeName attribute. You can provide a descriptive, localized, representation of this name by providing a corresponding entry in the InfoPlist.strings file(s). For example, given an Info.plist file that contains the following fragment:
<dict> |
<key>CFBundleDocumentTypes</key> |
<array> |
<dict> |
<key>CFBundleTypeName</key> |
<string>BinaryFile</string> |
<key>CFBundleTypeExtensions</key> |
<array> |
<string>binary</string> |
</array> |
you could provide a descriptive name by adding an entry in the InfoPlist.strings file:
BinaryFile = "Binary file format"; |
NSDocumentController.h
Returns the NSDocument subclass associated with a given document type.
- (Class)documentClassForType:(NSString *)documentTypeName
The name of a document type, specified by CFBundleTypeName in the application’s Info.plist file.
The document type must be one the receiver can read.
Returns the NSDocument subclass associated with documentTypeName. If the class cannot be found, returns nil.
Para
NSDocumentController.hReturns the names of NSDocument subclasses supported by this application.
- (NSArray *)documentClassNames
The names of NSDocument subclasses supported by this application.
The default implementation of this method returns information derived from the application's Info.plist property list file. You can override it to return the names of document classes that are dynamically loaded from plugins.
NSDocumentController.hReturns, for a given URL, the open document whose file or file package is located by the URL, or nil if there is no such open document.
- (id)documentForURL:(NSURL *)absoluteURL
The default implementation of this method queries each open document to find one whose URL matches, and returns the first one whose URL does match.
For backward binary compatibility with Mac OS X v10.3 and earlier, the default implementation of this method instead invokes documentForFileName: if it is overridden and the URL uses the file: scheme.
NSDocumentController.h
Returns the document object whose window controller owns a specified window.
- (id)documentForWindow:(NSWindow *)window
The document object whose window controller owns window. Returns nil if window is nil, if window has no window controller, or if the window controller does not have an association with an instance of NSDocument.
NSDocumentController.hReturns the NSDocument objects managed by the receiver.
- (NSArray *)documents
The NSDocument objects managed by the receiver. If there are currently no documents, returns an empty NSArray object.
NSDocumentController.h
Returns a Boolean value that indicates whether the receiver has any documents with unsaved changes.
- (BOOL)hasEditedDocuments
YES if the receiver has any documents with unsaved changes, otherwise NO.
NSDocumentController.hThis method is the designated initializer for NSDocumentController.
- (id)init
The first instance of NSDocumentController or any of its subclasses that is created becomes the shared instance.
NSDocumentController.hInstantiates a document located by a URL, of a specified type, but by reading the contents for the document from another URL, and returns it if successful.
- (id)makeDocumentForURL:(NSURL *)absoluteDocumentURL withContentsOfURL:(NSURL *)absoluteDocumentContentsURL ofType:(NSString *)typeName error:(NSError **)outError
The URL is specified by absoluteDocumentURL, the type by typeName, and the other URL providing the contents by absoluteDocumentContentsURL. If not successful, the method returns nil after setting outError to point to an NSError object that encapsulates the reason why the document could not be instantiated. The default implementation of this method invokes documentClassForType: to find out the class of document to instantiate, allocates a document object, and initializes it by sending it an initForURL:withContentsOfURL:ofType:error: message.
NSDocumentController.hInstantiates a document located by a URL, of a specified type, and returns it if successful.
- (id)makeDocumentWithContentsOfURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError
The URL is specified by absoluteURL and the document type by typeName. If not successful, the method returns nil after setting outError to point to an NSError that encapsulates the reason why the document could not be instantiated. The default implementation of this method invokes documentClassForType: to find out the class of document to instantiate, allocates a document object, and initializes it by sending it an initWithContentsOfURL:ofType:error: message.
For backward binary compatibility with Mac OS X v10.3 and earlier, the default implementation of this method instead invokes makeDocumentWithContentsOfFile:ofType: if it is overridden and the URL uses the file: scheme.
NSDocumentController.hInstantiates a new untitled document of the specified type and returns it if successful.
- (id)makeUntitledDocumentOfType:(NSString *)typeName error:(NSError **)outError
The document type is specified by typeName. If not successful, the method returns nil after setting outError to point to an NSError object that encapsulates the reason why a new untitled document could not be instantiated. The default implementation of this method invokes documentClassForType: to find out the class of document to instantiate, then allocates and initializes a document by sending it initWithType:error:.
For backward binary compatibility with Mac OS X v10.3 and earlier, the default implementation of this method instead invokes makeUntitledDocumentOfType: if it is overridden.
NSDocumentController.hReturns the maximum number of items that may be presented in the standard Open Recent menu.
- (NSUInteger)maximumRecentDocumentCount
A value of 0 indicates that NSDocumentController will not attempt to add an Open Recent menu to your application's File menu, although NSDocumentController will not attempt to remove any preexisting Open Recent menu item. The default implementation returns a value that is subject to change and may or may not be derived from a setting made by the user in System Preferences.
NSDocumentController.h
An action method invoked by the New menu command, this method creates a new NSDocument object and adds it to the list of such objects managed by the receiver.
- (IBAction)newDocument:(id)sender
It invokes openUntitledDocumentAndDisplay:error: with the document type (first argument) being the first one specified in the CFBundleDocumentTypes property (defined in Info.plist); the document type determines the NSDocument subclass used to instantiate the document object.
NSDocumentController.hThis method is called by NSDocument objects at appropriate times for managing the recent-documents list.
- (void)noteNewRecentDocument:(NSDocument *)aDocument
This method constructs a URL and calls noteNewRecentDocumentURL:. Subclasses might override this method to prevent certain documents or kinds of documents from getting into the list.
NSDocumentController.hThis method should be called by applications not based on NSDocument when they open or save documents identified by the given URL.
- (void)noteNewRecentDocumentURL:(NSURL *)aURL
NSDocument automatically calls this method when appropriate for NSDocument-based applications. Applications not based on NSDocument must also implement the application:openFile: method in the application delegate to handle requests from the Open Recent menu command. You can override this method in an NSDocument-based application to prevent certain kinds of documents from getting into the list (but you have to identify them by URL).
NSDocumentController.h
An action method invoked by the Open menu command, it runs the modal Open panel and, based on the selected filenames, creates one or more NSDocument objects from the contents of the files.
- (IBAction)openDocument:(id)sender
The method adds the newly created objects to the list of NSDocument objects managed by the receiver. This method invokes openDocumentWithContentsOfURL:display:error:, which actually creates the NSDocument objects.
NSDocumentController.hOpens a document located by the given URL presents its user interface if requested, and returns the document if successful.
- (id)openDocumentWithContentsOfURL:(NSURL *)absoluteURL display:(BOOL)displayDocument error:(NSError **)outError
If not successful, the method returns nil after setting outError to point to an NSError object that encapsulates the reason why the document could not be opened.
The default implementation of this method checks to see if the document is already open according to documentForURL:, and if it is not open determines the type of the document, invokes makeDocumentWithContentsOfURL:ofType:error: to instantiate it, then invokes addDocument: to record its opening, and sends the document makeWindowControllers and showWindows messages if displayDocument is YES. If the document is already open it is just sent a showWindows message if displayDocument is YES.
For backward binary compatibility with Mac OS X v10.3 and earlier, the default implementation of this method instead invokes openDocumentWithContentsOfFile:display:, if it is overridden and the URL uses the file: scheme.
NSDocumentController.hCreates a new untitled document, presents its user interface if displayDocument is YES, and returns the document if successful.
- (id)openUntitledDocumentAndDisplay:(BOOL)displayDocument error:(NSError **)outError
If not successful, the method returns nil after setting outError to point to an NSError that encapsulates the reason why a new untitled document could not be created.
The default implementation of this method invokes defaultType to determine the type of new document to create, invokes makeUntitledDocumentOfType:error: to create it, then invokes addDocument: to record its opening. If displayDocument is YES, it then sends the new document makeWindowControllers and showWindows messages.
For backward binary compatibility with Mac OS X v10.3 and earlier, the default implementation of this method instead invokes openUntitledDocumentOfType:display: if it is overridden.
NSDocumentController.hPresents an error alert to the user as a modal panel.
- (BOOL)presentError:(NSError *)error
Returns YES if error recovery was done, NO otherwise. This method does not return until the user dismisses the alert and, if the error has recovery options and a recovery delegate, the error's recovery delegate is sent an attemptRecoveryFromError:optionIndex: message.
The default NSDocumentController implementation of this method is equivalent to that of NSResponder while treating the application object as the next responder and forwarding error presentation messages to it. (The default NSDocument implementation of this method treats the shared NSDocumentController instance as the next responder and forwards these messages to it.) The default implementations of several NSDocumentController methods invoke this method.
The default implementation of this method invokes willPresentError: to give subclasses an opportunity to customize error presentation. You should not override this method but should instead override willPresentError:.
NSDocumentController.hPresents an error alert to the user as a modal panel.
- (void)presentError:(NSError *)error modalForWindow:(NSWindow *)window delegate:(id)delegate didPresentSelector:(SEL)didPresentSelector contextInfo:(void *)contextInfo
When the user dismisses the alert and any recovery possible for the error and chosen by the user has been attempted, sends the message didPresentSelector to the specified delegate. The method selected by didPresentSelector must have the same signature as:
- (void)didPresentErrorWithRecovery:(BOOL)didRecover contextInfo:(void *)contextInfo; |
The default NSDocumentController implementation of this method is equivalent to that of NSResponder while treating the application object as the next responder and forwarding error presentation messages to it. (The default NSDocument implementation of this method treats the shared NSDocumentController instance as the next responder and forwards these messages to it.)
The default implementation of this method invokes willPresentError: to give subclasses an opportunity to customize error presentation. You should not override this method but should instead override willPresentError:.
NSDocumentController.hReturns the list of recent-document URLs.
- (NSArray *)recentDocumentURLs
This method is not a good one to override since the internals of NSDocumentController do not generally use it.
NSDocumentController.hRemoves the given document from the list of open documents.
- (void)removeDocument:(NSDocument *)document
A document will automatically call removeDocument: when it closes. This method is mostly provided for subclasses that want to know when documents close.
NSDocumentController.hReopens an autosaved document located by a URL, by reading the contents for the document from another URL, presents its user interface, and returns YES if successful.
- (BOOL)reopenDocumentForURL:(NSURL *)absoluteDocumentURL withContentsOfURL:(NSURL *)absoluteDocumentContentsURL error:(NSError **)outError
The document is located by absoluteDocumentURL and the contents are read from absoluteDocumentContentsURL. If not successful, the method returns NO after setting outError to point to an NSError object that encapsulates the reason why the document could not be reopened.
NSDocumentController.hDisplays an alert dialog asking if the user wants to review unsaved documents (only if there are two or more unsaved documents), quit regardless of unsaved documents, or (if the choice is allowed) cancel the impending save operation.
- (void)reviewUnsavedDocumentsWithAlertTitle:(NSString *)title cancellable:(BOOL)cancellable delegate:(id)delegate didReviewAllSelector:(SEL)didReviewAllSelector contextInfo:(void *)contextInfo
Assigns delegate to the panel. Invokes didReviewAllSelector with YES if quit without saving is chosen or if there are no dirty documents, and NO otherwise. If the user selects the “Review Unsaved” option, closeAllDocumentsWithDelegate:didCloseAllSelector:contextInfo: is invoked. This method is invoked when the user chooses the Quit menu command, and also when the computer power is being turned off. Note that title is ignored. Pass the contextInfo object with the callback.
The didReviewAllSelector callback method should have the following signature:
- (void)documentController:(NSDocumentController *)docController didReviewAll: (BOOL)didReviewAll contextInfo:(void *)contextInfo |
NSDocumentController.hInvokes the NSOpenPanel runModalForTypes: method, passing the openPanel object and the file extensions associated with a document type.
- (NSInteger)runModalOpenPanel:(NSOpenPanel *)openPanel forTypes:(NSArray *)extensions
This method is invoked by the fileNamesFromRunningOpenPanel method. extensions may also contain encoded HFS file types as well as filename extensions.
NSDocumentController.h
As the action method invoked by the Save All command, saves all open documents of the application that need to be saved.
- (IBAction)saveAllDocuments:(id)sender
– saveDocument: (NSDocument)NSDocumentController.hSets the time interval in seconds for periodic autosaving.
- (void)setAutosavingDelay:(NSTimeInterval)autosavingDelay
A value of 0 indicates that periodic autosaving should not be done at all. NSDocumentController uses this number as the amount of time to wait between detecting that a document has unautosaved changes and sending the document an autosaveDocumentWithDelegate:didAutosaveSelector:contextInfo: message. The default value is 0.
NSDocumentController.hReturns, for a specified URL, the name of the document type that should be used when opening the document at that location, if successful.
- (NSString *)typeForContentsOfURL:(NSURL *)inAbsoluteURL error:(NSError **)outError
The URL is represented by absoluteURL. If not successful, the method returns nil after setting outError to point to an NSError object that encapsulates the reason why the document type could not be determined, or the fact that the document type is unrecognized.
You can override this method to customize type determination for documents being opened.
NSDocumentController.hCreates an NSOpenPanel instance and initializes it appropriately.
- (NSArray *)URLsFromRunningOpenPanel
This method uses runModalOpenPanel:forTypes: to run the open panel. Returns the chosen files as an array of URLs. Returns nil if the user cancels the Open panel or makes no selection.
NSDocumentController.hReturns a Boolean value that indicates whether a given user interface item should be enabled.
- (BOOL)validateUserInterfaceItem:(id < NSValidatedUserInterfaceItem >)anItem
YES if anItem should be enabled, otherwise NO.
Subclasses can override this method to perform additional validations. Subclasses should call super in their implementation for items they don’t handle themselves.
NSDocumentController.hCalled when the receiver is about to present an error, returns the error that should actually be presented.
- (NSError *)willPresentError:(NSError *)error
The default implementation of this method merely returns the passed-in error. The returned error may simply be forwarded to the application object.
You can override this method to customize the presentation of errors by examining the passed-in error and, for example, returning more specific information. When you override this method always check the NSError object's domain and code to discriminate between errors whose presentation you want to customize and those you don't. For errors you don't want to customize, call the superclass implementation, passing the original error.
NSDocumentController.h
Last updated: 2008-10-15