kmurp62rulz
kmurp62rulz

Reputation: 259

SwiftUI Textfield not actually making updates to data

I am trying to make an application where the user can see a list of foods and recipes and make changes etc. In the DetailView for each food, there is an EditView where the values can be changed. I am trying to use a double binding on the value by using @State to define the value and $food.name for example to make the changes happen. When I click 'Done' and exit this view back to the DetailView, the changes are not made at all, leaving me confused?

Any help on this problem would be greatly appreciated, thank you :)

My EditView.swift:

import SwiftUI

struct EditView: View {
    
    @State var food: Food
    var body: some View {
        
        List {
            
            Section(header: Text("Name")) {
                TextField("Name", text: $food.name)
            }
            Section(header: Text("Image")) {
                TextField("Image", text: $food.image)
            }
            Section(header: Text("Desc")) {
                TextField("Desc", text: $food.desc)
            }
            Section(header: Text("Story")) {
                TextField("Story", text: $food.story)
            }
            
        }.listStyle(InsetGroupedListStyle())
    }
}

struct EditView_Previews: PreviewProvider {
    static var previews: some View {
        EditView(food: cottonCandy)
    }
}

My FoodDetail.swift:

import SwiftUI

struct FoodDetail: View {
    @State var food: Food
    @State private var isPresented = false
    var body: some View {
        
        VStack {
            Image(food.image)
                .resizable()
                .frame(width: 300.0,height:300.0)
                .aspectRatio(contentMode: .fill)
                .shadow(radius: 6)
                .padding(.bottom)
            ScrollView {
                VStack(alignment: .leading) {
                    Text(food.name)
                        .font(.largeTitle)
                        .fontWeight(.bold)
                        .padding(.leading)
                        .multilineTextAlignment(.leading)
                    
                    Text(food.desc)
                        .italic()
                        .fontWeight(.ultraLight)
                        .padding(.horizontal)
                        .multilineTextAlignment(.leading)
                    Text(food.story)
                        .padding(.horizontal)
                        .padding(.top)
                    
                    Text("Ingredients")
                        .bold()
                        .padding(.horizontal)
                        .padding(.vertical)
                    ForEach(food.ingredients, id: \.self) { ingredient in
                        Text(ingredient)
                        Divider()
                    }.padding(.horizontal)
                    Text("Recipe")
                        .bold()
                        .padding(.horizontal)
                        .padding(.vertical)
                    ForEach(food.recipe, id: \.self) { step in
                        Text(step)
                        Divider()
                    }.padding(.horizontal)
                    
                }.frame(maxWidth: .infinity)
                
            }.frame(minWidth: 0,
                    maxWidth: .infinity,
                    maxHeight: .infinity,
                    alignment: .center
            )
        }
        .navigationBarItems(trailing: Button("Edit") {
            isPresented = true
        })
        
        .fullScreenCover(isPresented: $isPresented) {
            NavigationView {
                EditView(food: food)
                    .navigationTitle(food.name)
                    .navigationBarItems(leading: Button("Cancel") {
                        isPresented = false
                    }, trailing: Button("Done") {
                        isPresented = false
                    })
            }
        }
    }
    
}

struct FoodDetail_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            FoodDetail(food: cottonCandy)
            
        }
    }
}

Upvotes: 1

Views: 47

Answers (1)

aheze
aheze

Reputation: 30228

Inside EditView, you want to have a Binding. Replace

@State var food: Food

with

@Binding var food: Food

... then, you'll need to pass it in:

.fullScreenCover(isPresented: $isPresented) {
    NavigationView {
        EditView(food: $food) /// make sure to have dollar sign
            .navigationTitle(food.name)
            .navigationBarItems(leading: Button("Cancel") {
                isPresented = false
            }, trailing: Button("Done") {
                isPresented = false
            })
    }
}

Upvotes: 1

Related Questions