简体   繁体   English

SwiftUI:从json分配绑定变量解析object

[英]SwiftUI: assign binding variable from json parsed object

I am trying to assign a value I fetch and parse from JSON to another view.我正在尝试将我从 JSON 获取和解析的值分配给另一个视图。

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.在此示例代码中,正确传递了标题(定义为“hi”)。 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.然而,我想要做的是将 Novitads.name..de 的值分配给标题变量,以便我可以在模式视图中使用它。

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))")我只是在 ModalView Text("(String(title))")中显示 self.$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并且您的ModalView声明为

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

    /// .. all other your code
}

Note: @State private var title = "hi" can be removed at all注意: @State private var title = "hi"完全可以去掉

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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