Home > other >  TextField repeating the input SwiftUI
TextField repeating the input SwiftUI

Time:07-04

I have a problem that when I write in the first textFiled it be repeated in the rest textfields, like it's shown in the picture:

enter image description here

And here is my code: First here is the model and viewModel:

struct  Person: Identifiable {
    var id: String
    var name : String
    var age : String
}

class PersonViewModel : ObservableObject{
  
    @Published var PersonArray = [Person]()
    
    func emptyPersonArray() {
        self.PersonArray.append(Person(id: "", name: "", age: ""))
    }
   
}

and here is my view:

struct ContentView: View {
    @ObservedObject var personViewModel = PersonViewModel()
   
    var body: some View {
        VStack{
            Button(action: {
                personViewModel.emptyPersonArray()
            
            }) {
                HStack {
                   
                    Text("Add")
                        .font(.title3)
                        .bold()
                }
            }
            .padding()
            .foregroundColor(Color.black)
        .frame(width: 150.72, height: 40)
        .background(RoundedRectangle(cornerRadius: 6).stroke(Color(red: 0.463, green: 0.483, blue: 1.034), lineWidth: 2))
            
            
        List{
            ForEach(personViewModel.PersonArray) {person in
                PersonView(name: person.name, age: person.age)
            }
        }
        }
    }
}

Finally, here is the PersonView:

struct PersonView: View {

    @State var name = ""
    @State var age = ""
    
    var body: some View {
        
        ZStack {
            RoundedRectangle(cornerRadius: 9)
                .fill(Color.white)
                .frame(width: 650, height: 180)
            
            VStack (alignment: .center) {
               
                  
                        Text("Person")
                            .font(.title3)
                            .fontWeight(.bold)
                          
                  
                VStack{
                Text("Enter Name:")
                    TextField("", text: $name)
                        
                        .frame(width: 289, height: 40.0)
                        .background(RoundedRectangle(cornerRadius: 6).stroke(Color.black))
                    
                Text("Enter Age:")
                    TextField("", text: $age).padding()
                        .frame(width: 289, height: 40.0)
                        .background(RoundedRectangle(cornerRadius: 6).stroke(Color.black))
                }   .padding(.leading)
              
            }
        }
    }
}

CodePudding user response:

Yes, because you are giving the same id to all the persons each time you instantiate a new object, and you basically end up modifying the same person. Try giving a UUID like this to the Person:

struct  Person: Identifiable {
    var id: UUID = UUID()
    var name : String
    var age : String
}

class PersonViewModel : ObservableObject{
    
    @Published var PersonArray = [Person]()
    
    func emptyPersonArray() {
        self.PersonArray.append(Person(name: "", age: ""))
    }
    
}
  • Related