如何在基于文档的MacOS应用中处理不同的文档类型?
如何在一个MacOS文档应用中处理两种不同的自定义文档类型?
从MacOS文档应用模板开始,我定义了两种类型,这些类型也在info.plist中注册。plist:
extension UTType {
static var test1: UTType {
UTType(exportedAs: "com.exapmple.test1")
}
}
extension UTType {
static var test2: UTType {
UTType(exportedAs: "com.example.test2")
}
}
apple “ noreferrer”>文档说:
您的应用程序可以通过添加其他文档来支持多种文档类型 文档组场景:
但是显示的示例只有一种可以创建的类型,另一种仅读取(编辑器模式)。
如果我在主应用结构中执行此操作(基本上是模板中的样板:
@main
struct MultipleDocumentsApp: App {
var body: some Scene {
DocumentGroup(newDocument: DocumentOne()) { file in
Content1View(document: file.$document)
}
DocumentGroup(newDocument: DocumentTwo()) { file in
Content2View(document: file.$document)
}
}
}
..所产生的新菜单看起来像这样,我只能创建类型1的文档:
不同的新菜单项,用于我如何实现的任何想法?
How do I handle two different custom document types in one macOS document app?
Starting from the macOS Document App template I define two types, which are also registered in the info.plist :
extension UTType {
static var test1: UTType {
UTType(exportedAs: "com.exapmple.test1")
}
}
extension UTType {
static var test2: UTType {
UTType(exportedAs: "com.example.test2")
}
}
Apple documentation says:
Your app can support multiple document types by adding additional
document group scenes:
But the shown example has only one type that can be created, the other is read only (Editor mode).
If I do this in the main app struct (which is basically boilerplate from the template:
@main
struct MultipleDocumentsApp: App {
var body: some Scene {
DocumentGroup(newDocument: DocumentOne()) { file in
Content1View(document: file.$document)
}
DocumentGroup(newDocument: DocumentTwo()) { file in
Content2View(document: file.$document)
}
}
}
..the resulting New menu looks like this, and I can only create documents of type 1:
Obviously I would need two different New... menu items for the two doc types. Any ideas how I can achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它仍然是基于
nsdocumentController
,因此Pros& cons也相同 - 我们对默认文档类型(对于其他所有内容)进行了自动处理 - 回到编码。因此,您所做的一切都是正确的,唯一的剩余就是在编程中添加其他(非默认)类型的新文档的创建。
这是方法的主要部分:
完成
It is still
NSDocumentController
based, so pros&cons are also the same - we have automatic handling for default document type, for everything else - back to coding.So all you've done is correct, the only left is to add, programmatically, creation of new document of other (non default) type.
Here is main part of approach:
Complete findings and code is here