I am trying to pass data through an @EnvironmentObject
, but it works only if I go to the next view through NavigationButton
, however, I want to present the next view modally ( 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)")
}
}
}
What I am trying to use :
PresentationButton( Text("Show Detail View"), destination: DetailView())
Try supplying the bindable object to the DetailView
using environmentObject
:
PresentationButton(Text("Show Detail View"),
destination: DetailView().environmentObject(settings))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.