从模态表示传递数据

时间:2019-10-16 21:31:48

标签: swift swiftui

如何将数据传递到“表示模式”视图,以及如何在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)

        }
    }
}

1 个答案:

答案 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)
    }
    // ...