Cannot pass data between views and also update in second view - SwiftUI

380 views Asked by At

I am trying to pass data between 2 views using @Published and @ObservedObject following this article, but for some reason I cannot pass the data through, the @Published just keeps at the initial state that it has been given instead of changing when the button is clicked.

The first view ProductList2 navigates to a tabView and I am trying to pass the data to one of the tabViews. Any help would be appreciated.

Here is my view where I am creating the ObservableObject and @Published

class selectedApplication: ObservableObject {
    @Published var selectedApplication = "All"
}

struct ProductList2: View {
    @ObservedObject var selectedOption = selectedApplication()
    var products: [ProductModel] = productData
    
    var body: some View {
        VStack {
            HStack {
                List{
                    ForEach(applicationsArray, id: \.self) { item in
                        Button(action: {
                            self.selectedOption.selectedApplication = item
                        }) {
                            HStack(){
                                Image(item)
                                Text(item)
                            }
                        }
                    }
                }
            }
            List{
                let matchedItems = products.filter {
                    product in
                    let list = product.application
                    for item in list {
                        if item == selectedOption.selectedApplication {
                            return true
                        }
                    }
                    return false
                }
                ForEach(matchedItems) { item in
                    NavigationLink(destination: ProductTabView(product: item)) {
                        ProductListRow(product: item)
                    }
                }
            }
        }
    }
}

This is the tabview view where I am trying to retrieve the data:

struct ProductTab5View: View {
    var product: ProductModel
    @ObservedObject private var application = selectedApplication()
    
    var body: some View {
        VStack(alignment: .leading){
            Text(product.detailTabNames[3])
            ScrollView(.horizontal, showsIndicators: false) {
                HStack(alignment: .center, spacing: 0){
                    ForEach(product.application, id: \.self) { item in
                            Button(action: {
                                application.selectedApplication = item
                            }) {
                                VStack {
                                    Image(item)
                                    Text(item)
                                }
                            }
                    }
                }
            }            
            VStack(alignment: .center){
                Text(application.selectedApplication)
            }
        } 
    }
}

EDIT:

I have updated the navigation link and @ObservedObject but still cannot get it working:

This is the updated ProductList2 NavigationLink:

ForEach(matchedItems) { item in
                        NavigationLink(destination: ProductTabView(product: item, application: selectedOption.selectedApplication)){
                            ProductListRow(product: item)
                        }
                    }

And this is the @ObservedObject on ProductTab5View, I also cannot get preview working:

@ObservedObject private var application: selectedApplication

struct ProductTab5View_Previews: PreviewProvider {
    static var previews: some View {
        ProductTab5View(product: productData[0], application: application)
    }
}
1

There are 1 answers

7
pawello2222 On BEST ANSWER

You're using two different instances of selectedApplication:

struct ProductList2: View {
    @ObservedObject var selectedOption = selectedApplication()
struct ProductTab5View: View {
    ...
    @ObservedObject private var application = selectedApplication()

You need to use the same instance in both views.

struct ProductTab5View: View {
    ...
    @ObservedObject var application: selectedApplication // declare only
// pass the already created instance to the child view
NavigationLink(destination: ProductTabView(product: item, application: selectedOption))