具有多种文档类型的基于 Cocoa 文档的应用程序

发布于 2024-08-17 16:14:55 字数 308 浏览 7 评论 0 原文

我想在 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉南桥 2024-08-24 16:14:55

这是 Cocoa Document 系统明确设计的目的。 Apple 提供文档,但以下是重点内容。

  • 每种文档都是 NSDocument 的子类。如果您使用的是 Core Data,请将您的类基于 NSPersistentDocument。 (Apple 有一个关于如何在基于文档的应用程序中使用 Core Data)
  • 您可以使用 Info.plist
  • 每个 NSDocument 子类都有一个或多个与其关联的 NSWindowController 对象,每个对象代表一个窗口。如果您只有一个窗口,则不必子类化 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.

  • Each kind of document is a subclass of NSDocument. If you're using Core Data, base your class on NSPersistentDocument instead. (Apple has a basic tutorial on how to use Core Data in a document-based application)
  • You tell Cocoa about the kinds of documents your application can open, and which document class to use, with the Info.plist.
  • Each NSDocument subclass has one or more NSWindowController objects associated with it, each of which represents a single window. If you'll only have a single window, you don't have to subclass NSWindowController. You can put your UI logic in your document subclass. However, for cleaner code, I'd strongly recommend subclassing NSWindowController.
  • NSWindowController (and NSDocument if you decide not to subclass NSWindowController) 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.

写给空气的情书 2024-08-24 16:14:55

您首先要为每种文档类型创建一个 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. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文