在SwiftUI 2.0中使用DocumentGroup时如何更改菜单命令和文本
我正在尝试使用Swiftui2.0DocumentGroup和MenuCommand功能。要向文档发送菜单命令,我使用PassthroughSubject发送命令,然后ContentView使用其.onReceive方法接收该命令,如下所示:在SwiftUI 2.0中使用DocumentGroup时如何更改菜单命令和文本,swiftui,combine,Swiftui,Combine,我正在尝试使用Swiftui2.0DocumentGroup和MenuCommand功能。要向文档发送菜单命令,我使用PassthroughSubject发送命令,然后ContentView使用其.onReceive方法接收该命令,如下所示: import SwiftUI import Combine @main struct TestApp: App { private let command = PassthroughSubject<String, Never>()
import SwiftUI
import Combine
@main
struct TestApp: App {
private let command = PassthroughSubject<String, Never>()
var body: some Scene {
DocumentGroup(viewing: TestDocument.self) { file in
ContentView(document: file.$document)
.onReceive(command) { command in
print(command) // send to file.document here for processing
}
} .commands {
CommandMenu("Video") {
Button("Play") {
command.send("Play")
}
}
}
}
}
导入快捷界面
进口联合收割机
@主要
struct TestApp:App{
private let命令=PassthroughSubject()
var body:一些场景{
DocumentGroup(查看:TestDocument.self){中的文件
ContentView(文档:file.$document)
.onReceive(command){command in
打印(命令)//发送到file.document此处进行处理
}
}.命令{
命令菜单(“视频”){
按钮(“播放”){
命令发送(“播放”)
}
}
}
}
}
这一切似乎都运作良好
但我的问题是,如何根据文档中的值修改菜单?在上面的例子中,假设我想根据文档布尔属性将菜单的文本从“Play”更改为“Stop”。由于菜单按钮无法访问文档,如何使其更新
谢谢你的帮助