我想在 Cocoa 中构建一个基于文档的应用程序,以便它可以创建和处理不同类型的文档。将 Word、Excel、Powerpoint 想象为一个应用程序,而且更加简单。但每个窗口都会根据文档类型而有所不同。
对于存储,我将使用 CoreData。我考虑添加一个指定文档类型的字段,因为它们都应该具有相同的文件结尾。
那么,在不创建多个独立应用程序的情况下,最好的方法是什么?如何在 Interface Builder 中创建它?我该如何编码?
我不需要详细的源代码或任何东西,只需要如何做到这一点的一般想法,我会弄清楚其余的。
提前致谢!
I want to build a document-based app in Cocoa but so that it can create and handle different types of documents. Think Word, Excel, Powerpoint all in one application, only much simpler. But every window will be different based on the type of document.
For storage I will use CoreData. I think of adding a field that specifies the type of document since they should all have the same file ending.
So without creating several independent apps what would be the best way to go about it? How do I create this in Interface Builder? How do I code this up?
I don't need detailed source code or anything, just the general idea of how to do this, I will figure the rest out.
Thanks in advance!
发布评论
评论(2)
这是 Cocoa Document 系统明确设计的目的。 Apple 提供文档,但以下是重点内容。
NSPersistentDocument
。 (Apple 有一个关于如何在基于文档的应用程序中使用 Core Data)NSWindowController
。您可以将 UI 逻辑放入文档子类中。但是,为了使代码更简洁,我强烈建议对NSWindowController
进行子类化。NSWindowController
(如果您决定不子类化NSWindowController
,还有NSDocument
)可以从您在 Interface Builder 中构建的 NIB 加载窗口。事实上,这是创建文档窗口的推荐方法。希望这能让您大致了解如何在 Cocoa 中实现这一点。
This is something the Cocoa Document system is explicitly designed to do. Apple provides documentation, but here are the highlights.
NSDocument
. If you're using Core Data, base your class onNSPersistentDocument
instead. (Apple has a basic tutorial on how to use Core Data in a document-based application)NSDocument
subclass has one or moreNSWindowController
objects associated with it, each of which represents a single window. If you'll only have a single window, you don't have to subclassNSWindowController
. You can put your UI logic in your document subclass. However, for cleaner code, I'd strongly recommend subclassingNSWindowController
.NSWindowController
(andNSDocument
if you decide not to subclassNSWindowController
) can load a window from a NIB you build in Interface Builder. In fact, this is the recommended approach for creating your document windows.Hopefully this gives you a general idea of how to approach this in Cocoa.
您首先要为每种文档类型创建一个 NIB;每个都有一个 NSDocument 子类(在此处使用现有的基于文档的应用程序示例 NIB 进行设置)。然后,您可以在应用程序的属性列表中设置这些类来处理各种文档类型;据我所知,XCode 中隐藏着一些有用的工具。
一旦开始运行,大部分细节都应该自动处理;但您可能仍然需要稍微修改一下“文件”菜单,而且我似乎记得设置默认文档类型等方面存在一些问题。
总的来说,这并不比为单一类型的文档创建应用程序复杂多少。
PS:请务必确保您确实需要不同的文件类型;有时,对同一文件有多个不同的视图可能更合适。只是一个想法。 :)
You would start by creating a NIB for each of your document types; and an
NSDocument
subclass for each (use an existing document-based-app-example-NIB for the setup here). You would then set theses classes to handle your various document types in the property list of the application; insofar as I remember, there are some useful tools for this hidden somewhere in XCode.Once that is up an running, most of the details should be handled automagically; but you might still have to fudge the File-menu about a bit, and I seem to remember there being some trouble with setting the default document type and whatnot.
On the whole, tho', it's not a lot more complex than creating an app for a single kind of documents.
PS: Do take care to ensure that you really need different filetypes; sometimes it might be more appropriate having several different views of the same file. Just a thought. :)