基于文档的应用程序,如何从菜单中访问文档? [英] Document based Apps, how to get access to the Document from within a menu?

查看:34
本文介绍了基于文档的应用程序,如何从菜单中访问文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Xcode 12 中使用了新的 MultiPlatform SwiftUI 文档模板,但我不明白如何从菜单项中访问当前的 FileDocument.

I am using the new MultiPlatform SwiftUI Document template in Xcode 12 and I don't understand how to get access to the current FileDocument from within a menu item.

我的应用代码如下所示(直接来自模板).

My app code looks like this (straight from the template).

@main
struct MyApp: App {
    var body: some Scene {
        DocumentGroup(newDocument: MyDocument()) { file in
            ContentView(document: file.$document)
        }
        .commands {
            CommandMenu("Utilities") {
                Button(action: {
                    // How to get access to the current FileDocument ?
                }) {
                    Text("Test")
                }
            }
        }
    }
}

推荐答案

您可以创建一个 FocusedValueKey,从您的内容视图发布到文档的绑定,然后在菜单中使用 <代码>@FocusedBinding.这里有很好的解释.

You can create a FocusedValueKey, publish a binding to the document from your content view, and then observe it in the menu using @FocusedBinding. There is a great explanation here.

这篇关于基于文档的应用程序,如何从菜单中访问文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆