简体   繁体   中英

SwiftUI: assign binding variable from json parsed object

I am trying to assign a value I fetch and parse from JSON to another view.

struct ContentView: View {
    @State private var showAlert = false
    @State private var showAbout = false
    @State private var showModal = false
    @State private var title = "hi"

    @State private var isCodeSelectorPresented = false

    @ObservedObject var fetch = FetchNovitads()



    var body: some View {
        VStack {
            NavigationView {
                List(fetch.Novitadss) { Novitads in
                    VStack(alignment: .leading) {
                        // 3.

                        Text(Novitads.name!.de)
                            .platformFont()
                            .fontWeight(.black)

                        Text(Novitads.textTeaser.de)
                            .platformFont()
                            .fontWeight(.medium)
                            .onTapGesture {
                                self.showModal.toggle()
                                // 3.
                            }.sheet(isPresented: self.$showModal) {
                                ModalView(showModal: self.$showModal,
                                          title: self.$title)
                            }

In this sample code the title (defined as "hi") is passed correctly. What I want to do however is to assign the value of Novitads.name..de to the title variable so that I can use it in the modal view.

try assigning the title like this:

struct ContentView: View {

struct ContentView: View {
@State private var showAlert = false
@State private var showAbout = false
@State private var showModal = false
@State private var title = "hi"

@State private var isCodeSelectorPresented = false
@ObservedObject var fetch = FetchNovitads()

    var body: some View {
        VStack {
            NavigationView {
                List(fetch.Novitadss) { Novitads in
                    VStack(alignment: .leading) {
                        // 3.

                        Text(Novitads.name!.de)
                            .platformFont()
                            .fontWeight(.black)

                        Text(Novitads.textTeaser.de)
                            .platformFont()
                            .fontWeight(.medium)
                            .onTapGesture {
                                self.showModal.toggle()
                                // 3.
                        }.sheet(isPresented: self.$showModal) {
                            ModalView(showModal: self.$showModal, title: self.$title)
                        }
                    }
                }
            }
        }.onAppear(perform:{ self.title = self.fetch.Novitads.name!.de })
    }

I just display self.$title in the ModalView Text("(String(title))")

Then you don't need binding here and pass value directly, like

ModalView(showModal: self.$showModal, title: Novitads.name!.de)

and your ModalView declaration be as

struct ModalView: View {
    @Binding showModal: Bool
    let title: String

    /// .. all other your code
}

Note: @State private var title = "hi" can be removed at all

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM