1

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.

2
  • What do you do with self.$title in ModalView? Do you really need binding here? BTW, sheet in List will not work as you expect - you have to move it out of List. Commented Apr 25, 2020 at 4:19
  • @Asperi I just display self.$title in the ModalView Text("(String(title))") Commented Apr 25, 2020 at 7:58

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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 })
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.