swift 如何在向类添加项目时更新@ObservedObject对象

cld4siwp  于 2023-01-25  发布在  Swift
关注(0)|答案(1)|浏览(121)

我有两个部分(配料选择和添加配料).配料选择中,我从一个json文件中获取所有项目,目标是每当我从配料选择中选择一个配料时,该项目都应该添加到添加配料部分,但实际结果是我可以添加AddedIngredients中的所有配料,但我的@ObservedObject var ingredientAdded :AddedIngredients显示没有项目,所以这部分什么都没有。
我的逻辑中遗漏了什么?是因为@Published var ingredients = [Ingredient]()来自一个每次都创建一个新结构的结构吗?我的理解是,由于AddedIngredients是一个类,它应该引用相同的日期,除非我覆盖它。
这是我的模型:

import Foundation

struct Ingredient: Codable, Identifiable {
    let id: String
    let price: String
    let ajoute: Bool
}

class AddedIngredients: ObservableObject {

    @Published var ingredients = [Ingredient]()
}

显示所有配料的配料部分:

struct SectionIngredientsSelection: View {
    let ingredients: [Ingredient]
    @StateObject var ajoute = AddedIngredients()
    var body: some View {
        Section(header: VStack {
            Text("Ajouter vos ingredients")

        }) {
            ForEach(ingredients){ingredient in
                HStack{
                    HStack{
                        Image("mais")
                            .resizable()
                            .frame(width: 20, height: 20)
                        Text(ingredient.id)
                    }
                    Spacer()
                    HStack{
                        Text(ingredient.price )
                        Button(action: {
                            ajoute.ingredients.append(ingredient)
                            print(ajoute.ingredients.count)
                        }){
                            Image(systemName: "plus")
                                .foregroundColor(Color(#colorLiteral(red: 0, green: 0.3257463574, blue: 0, alpha: 1)))
                        }
                    }
                }
            }
            .listRowBackground(Color.white)
            .listRowSeparator(.hidden)
        }
    }
}

每当我从上一节添加一种成分时,它就应该出现在这一节。

struct SectionIngredientsSelected: View {
    @ObservedObject var ingredientAdded :AddedIngredients
        var body: some View {
            Section(header: VStack {
                HStack{
                    Text("Vos ingredients")
                        .textCase(nil)
                        .font(.headline)
                        .fontWeight(.bold)
                        .foregroundColor(.black)

                    Button(action: {print(ingredientAdded.ingredients.count)}, label: {Text("Add")})
                }
            }) {
                ForEach(ingredientAdded.ingredients){ingredient in
                    HStack{
                        HStack{
                            Image("mais")
                                .resizable()
                                .frame(width: 20, height: 20)
                            Text(ingredient.id)
                        }
                        Spacer()
                        HStack{
                            Text(ingredient.price )
                            Button(action: {
                            }){
                                Image(systemName: "xmark.circle")
                                    .foregroundColor(Color(#colorLiteral(red: 0, green: 0.3257463574, blue: 0, alpha: 1)))
                            }
                        }
                    }
                }
                .listRowBackground(Color.white)
                .listRowSeparator(.hidden)
            }
        }
    }

主视图

struct CreationView: View {
    let ingredients: [Ingredient] = Bundle.main.decode("Ingredients.json")
    var addedIngre: AddedIngredients
    var body: some View {
        ScrollView {
            VStack{
                VStack(alignment: .leading) {
                    Image("creation")
                        .resizable()
                        .scaledToFit()
                    Text("Slectionnez vos ingredients preferes").fontWeight(.light)
                    Divider()
                        .padding(.bottom)
                }
            }
        }
        List {
            SectionIngredientsSelected(ingredientAdded: addedIngre)
            SectionIngredientsSelection(ingredients: ingredients)

        }
    }
}
3lxsmp7m

3lxsmp7m1#

您正在创建AddedIngredients的两个不同示例,它们的内容彼此不同步,最简单的解决方案是将AddedIngredients向上拉到CreationView中,并将其向下传递给子视图。
因此,请将CreationView更改为:

struct CreationView: View {
    let ingredients: [Ingredient] = Bundle.main.decode("Ingredients.json")
    @StateObject private var addedIngre: AddedIngredients = AddedIngredients()

以及:

SectionIngredientsSelected(ingredientAdded: addedIngre)
SectionIngredientsSelection(ingredients: ingredients, ajoute: addedIngre)

SectionIngredientsSelection到:

struct SectionIngredientsSelection: View {
    let ingredients: [Ingredient]
    @ObservedObject var ajoute: AddedIngredients

备注:
我不太清楚CreationView是如何获取addedIngre的。从你的代码看,它似乎是以某种方式被注入的。如果是这种情况,并且你需要它向上的视图层次结构,将@StateObject更改为@ObservedObject。但要确保它是用@StateObject初始化的。

相关问题