我可以在 SwiftUI 应用程序中针对不同文档类型使用两个不同的 DocumentGroup 元素吗?
根据文档,
您的应用可以通过添加额外的文档组场景来支持多种文档类型
因此我有两种可编辑的不同文档类型(一个裸文档和一组带有元数据的此类文档),因此我有两个 DocumentGroup
元素:
var body: some Scene {
DocumentGroup {
BundledDocument()
} editor: { file in
BundleView(document: file.document)
}
.commands {
...Commands...
}
DocumentGroup {
BareDocument()
} editor: { file in
BareDocumentView(document: file.document)
}
WindowGroup("otherWindow") {
OtherWindow()
}
问题是“文件”菜单有一个“新建”子菜单,只有一个“新建文档”条目,并且它只创建列出的第一个文档类型(我已经尝试了这两个命令)。
那么,有没有办法允许用户指定一种文档类型或另一种文档类型来创建新文档呢?
我尝试添加
.handlesExternalEvents(matching: Set(arrayLiteral: "newDoc"))
到第二个 DocumentGroup
并添加一个菜单命令以将 openURL
与“newDoc”URL 一起使用,但随后它会打开一个“otherWindow”类型的新窗口。如果我随后向该 WindowGroup 添加另一条类似的行,则菜单命令不会打开任何类型的窗口。
第二个 DocumentGroup
显然可以打开正确类型的文件,因此它有一定用处。然而,在我看来,我似乎无法让它以这种方式生成新文档。
我遇到的唯一其他替代方案是自己创建窗口,或者在临时文件中创建新文档,然后提供该文件的 URL。我想,这两者都不是特别有吸引力,但也是可能的。
更新: 我实现了创建临时文件,然后使用 NSWorkspace.shared.open 打开它的选项,这可以工作,但留下的文件可能不在用户想要的位置,因为应用程序是沙盒的,并且它最终进入容器。但它确实有效,而且在 SwiftUI 开发的现阶段我似乎无法做得更好。
According to the documentation,
Your app can support multiple document types by adding additional document group scenes
So I have two different document types (a bare document and a bundle of such documents with metadata) that are editable, so I have two DocumentGroup
elements:
var body: some Scene {
DocumentGroup {
BundledDocument()
} editor: { file in
BundleView(document: file.document)
}
.commands {
...Commands...
}
DocumentGroup {
BareDocument()
} editor: { file in
BareDocumentView(document: file.document)
}
WindowGroup("otherWindow") {
OtherWindow()
}
The problem is that the File menu has a New submenu, with only one New Document entry, and it only creates the first document type listed (I've tried both orders).
So, is there a way to allow the user to specify one document type or the other to create a new document?
I tried adding
.handlesExternalEvents(matching: Set(arrayLiteral: "newDoc"))
to the second DocumentGroup
and adding a menu command to use openURL
with the "newDoc" URL, but it then opens a new window of the "otherWindow" type. If I then add another similar line to that WindowGroup
, then the menu command doesn't open either type of window.
The second DocumentGroup
does apparently work for opening a file of the correct type, so it is of some use. However, it looks to me as though I cannot make it produce a new document this way.
The only other alternatives I've come across are to create the window myself, or to create a new document in a temporary file and then feed the URL for that file. Neither is particularly attractive, but possible, I suppose.
Update:
I implemented the option of creating a temporary file and then using NSWorkspace.shared.open
to open it, which works, but leaves a file probably not where the user wants it, as the app is sandboxed, and it ends up in the container. But it works, and I can't do better at this stage of SwiftUI's development, it seems.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论