简体   繁体   English

基于可可文档的应用程序:仅查看者文件类型的“保存”更改为“另存为”

[英]Cocoa Document-Based App: Change “Save” to “Save As” for Viewer-Only Filetypes

I have a Cocoa document-based app that (currently at least) functions as a basic text editor. 我有一个基于Cocoa文档的应用程序(当前至少)用作基本文本编辑器。 It saves .txt, .rtf, and .rtfd, and loads those plus .doc and .docx. 它保存.txt,.rtf和.rtfd,并加载它们以及.doc和.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. 如果打开.doc或.docx文件并进行编辑,然后尝试关闭,它会提示我进行保存,但是save选项不会执行任何操作,因为该应用程序只是这些文件类型的查看器。 How can I make that function as "Save As" for types that can only be viewed, like .doc and .docx? 对于只能查看的类型(如.doc和.docx),如何使该功能用作“另存为”?

Override the saveDocumentWithDelegate::: in your customised NSDocument to the following: 将自定义的NSDocumentsaveDocumentWithDelegate:::重写为以下内容:

- (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. 默认情况下, delegate是关闭窗口上的NSWindowNSDocumentController如果您退出应用程序且控制器枚举窗口以供查看更改)。

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 然后,您需要1.确保文档的内容没有更改,和/或2.告诉文档不要打扰自己显示为脏内容。

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. 除了TextEdit的源代码可用并包含在Xcode中之外,这并不是一件容易的事。 But from a cursory glance, it appears that NSDocument already supports .doc and .docx. 但是从粗略地看,NSDocument似乎已经支持.doc和.docx。

You will find the Project folder for TextEdit in /Xcode/Examples. 您可以在/ Xcode / Examples中找到TextEdit的Project文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM