在 SwiftUI 中将 @EnvironmentObject 与 PresentationButton 结合使用

Use @EnvironmentObject with PresentationButton in SwiftUI

我正在尝试通过 @EnvironmentObject 传递数据,但只有当我通过 NavigationButton 转到下一个视图时它才有效,但是,我想以模态方式呈现下一个视图(PresentationButton)

struct ContentView : View {

     @EnvironmentObject var settings: UserSettings

     var body: some View {
        NavigationView {
        VStack {
            // A button that writes to the environment settings
            Button(action: {
                self.settings.score += 1
            }) {
                Text("Increase Score")
            }
            NavigationButton(destination: DetailView()) {
                Text("Show Detail View")
            }
        }
      }
   }
}

struct DetailView: View {
     @EnvironmentObject var settings: UserSettings

     var body: some View {
     // A text view that reads from the environment settings
        VStack {
            Text("Score: \(settings.score)")        
        }
     }
 }

我正在尝试使用什么:

 PresentationButton( Text("Show Detail View"), destination: DetailView())

尝试使用 environmentObjectDetailView 提供可绑定对象:

PresentationButton(Text("Show Detail View"), 
                   destination: DetailView().environmentObject(settings))