我正试图通过@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())
最佳答案
尝试使用DetailView
将可绑定对象提供给environmentObject
:
PresentationButton(Text("Show Detail View"),
destination: DetailView().environmentObject(settings))
关于ios - 在SwiftUI中将@EnvironmentObject与PresentationButton一起使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56593814/