简体   繁体   English

TextField 重复输入 SwiftUI

[英]TextField repeating the input SwiftUI

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:我有一个问题,当我在第一个 textFiled 中写入时,它会在 rest 文本字段中重复,如图所示:

在此处输入图像描述

And here is my code: First here is the model and viewModel:这是我的代码:首先是 model 和 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 :最后,这是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)
              
            }
        }
    }
}

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.是的,因为每次实例化一个新的 object 时,您都给所有人提供了相同的 id,并且您基本上最终修改了同一个人。 Try giving a UUID like this to the Person :尝试将这样的UUID提供给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: ""))
    }
    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM