Home > database >  How to bind a list of textfields that edit a variable within an a core data array?
How to bind a list of textfields that edit a variable within an a core data array?

Time:04-09

swift ui requires a Binding to link to the value you are updating in a text field. Much like the native iPhone Reminders app, I am looking to permit inline editing a list that will persist. The attached code works only but gives the same name for each item due to them all being bound to the same variable. How can I bind this to the [FruitEntity] array?

Thank you, I have been stuck for days!

class CoreDataViewModel: ObservableObject {
    
    //static let instance = CoreDataViewModel()
    let container: NSPersistentContainer
    let context: NSManagedObjectContext
    @Published var savedEntities: [FruitEntity] = []
}



struct Screen: View {
    
    @StateObject var vm = CoreDataViewModel()
    
    var body: some View {
        List{
            ForEach(vm.savedEntities, id: \.self) {entity in
                VStack{
                    HStack {
                        TextField("\(entity.name ?? "Workout Name...")", text: $questionVariable)
                            .onChange(of: entity.name) { text in
                                entity.name = questionVariable
                            }
                    }
                    .onDelete(perform: vm.deleteFruit)
                    .onMove(perform: moveItem)
                }
            }
        }
    }
}

CodePudding user response:

You can just move the TextField to a separate view, with its own @State var for the field and another var for the entity.

Create a view like the following one:

struct ChangeName: View {

    // Will change the entity
    let entity: FruitEntity

    // Will update the field
    @State private var questionVariable = ""

    var body: some View {

                TextField("\(entity.name ?? "Workout Name...")", text: $questionVariable)
                    .onChange(of: questionVariable) { text in
                        entity.name = text

                        // Remember to save the persistent container/ managed-object-context
                    }
    }
}

Call it in your main view:

struct Screen: View {

    List{
        ForEach(vm.savedEntities, id: \.self) {entity in
            VStack{
                HStack {
                    ChangeName(entity: entity)
                }

            }
            .onDelete(perform: vm.deleteFruit)
            .onMove(perform: moveItem)
         }
     }
  }
  • Related