开发者

Cocoa Document-Based App: Change "Save" to "Save As" for Viewer-Only Filetypes

开发者 https://www.devze.com 2023-01-08 02:45 出处:网络
I have a Cocoa document-based app that (currently at least) functions as a basic text editor.It saves .txt, .rtf, and .rtfd, and loads t开发者_JAVA技巧hose plus .doc and .docx.If I open a .doc or .doc

I have a Cocoa document-based app that (currently at least) functions as a basic text editor. It saves .txt, .rtf, and .rtfd, and loads t开发者_JAVA技巧hose plus .doc and .docx. If I open a .doc or .docx file and edit it, then try to close, it reminds me to save, but the save option doesn't do anything since the application is only a viewer for those types of files. How can I make that function as "Save As" for types that can only be viewed, like .doc and .docx?


Override the saveDocumentWithDelegate::: in your customised NSDocument to the following:

- (void)saveDocumentWithDelegate:(id)delegate didSaveSelector:(SEL)didSaveSelector contextInfo:(void *)contextInfo
{
  if (delegate != nil)
  {
    // use delegate or contextInfo to decide what operation you need to use...

    [self runModalSavePanelForSaveOperation:NSSaveAsOperation
                                   delegate:delegate 
                            didSaveSelector:didSaveSelector 
                                contextInfo:contextInfo];
  }
  else
  {
    [super saveDocumentWithDelegate:delegate 
                    didSaveSelector:didSaveSelector
                        contextInfo:contextInfo];
  }
}

By default the delegate is either an NSWindow on window close or NSDocumentController if you quit the application and the controller enumerates the windows for reviewing changes.


Not completely clear if you actually want to write the updated file after editing, or prevent editing and thus prevent the warning that the document has been modified.

To not see the Save warning, first you would want to set your document type role to "Viewer", if it happens to be "Editor". This is in the Target settings.

Then you need to 1. ensure that the contents of the document isn't changed, and/or 2. tell the document not to bother showing itself as dirty

However, if you want to allow editing and saving the document, you would have to write those files back in the proper format. That's non-trivial, except for the fact that the source code for TextEdit is available and included with Xcode. But from a cursory glance, it appears that NSDocument already supports .doc and .docx.

You will find the Project folder for TextEdit in /Xcode/Examples.

0

精彩评论

暂无评论...
验证码 换一张
取 消