Reputation: 637
How to pass data to a "presentation modal" view and that data can be retrieved in Detail
I need to pass the variable title
to Detail ()
struct ContentView: View {
@State var showingDetail = false
let title = "My Title"
var body: some View {
Button(action: {
self.showingDetail.toggle()
}) {
Text("Show Detail")
}.sheet(isPresented: $showingDetail) {
Detail()
}
}
}
struct Detail: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var body: some View {
NavigationView {
ScrollView {
VStack {
Text("Details view")
Text("Details view")
}
}
.navigationBarTitle("Booking", displayMode: .inline)
.navigationBarItems(trailing:
Button(action: {
self.presentationMode.wrappedValue.dismiss()
print("close")
}) { Image(systemName: "xmark") }).accentColor(.pink)
}
}
}
Upvotes: 2
Views: 836
Reputation: 14388
Just declare it as a viariable/constant on Detail like this:
struct Detail: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
let title: String
var body: some View {
NavigationView {
ScrollView {
VStack {
Text(title)
//...end so on
and then pass it into the initialiser in ConotentView:
struct ContentView: View {
//...
}.sheet(isPresented: $showingDetail) {
Detail(title: self.title)
}
// ...
Upvotes: 4