如何将数据传递到“表示模式”视图,以及如何在Detail
中检索数据
我需要将变量title
传递给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)
}
}
}
答案 0 :(得分:2)
只需在Detail上将其声明为可更改/常量即可:
struct Detail: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
let title: String
var body: some View {
NavigationView {
ScrollView {
VStack {
Text(title)
//...end so on
,然后将其传递到ConotentView中的初始化程序中:
struct ContentView: View {
//...
}.sheet(isPresented: $showingDetail) {
Detail(title: self.title)
}
// ...