I worked it out: was an issue with the button action. See full code below:
import SwiftUI
struct ContentView: View {
@State private var persons: [Person] = []
var body: some View {
VStack{
ForEach($persons) { $person in TextField("Name", text: $person.name) }
Button(action: {
persons.append(Person.init(name: ""))
}) {
Text("Add Name")
Image(systemName: "plus.circle.fill")
.foregroundColor(Color(.systemGreen))
}
.padding()
}
Spacer()
.frame(width: 600, height: 600)
}
}
struct Person: Identifiable {
var id = UUID()
var name: String
}
Thanks again!
Post
Replies
Boosts
Views
Activity
Hello Polyphonic,
Thank you for your response. I have followed your input and appreciate your guidance. I have adjusted my code to the following:
import SwiftUI
struct ContentView: View {
@State private var persons: [Person] = []
var body: some View {
VStack{
ForEach($persons) { $person in TextField("Name", text: $person.name) }
Button(action: {
persons.append(contentsOf: persons)
}) {
Text("Add Name")
Image(systemName: "plus.circle.fill")
.foregroundColor(Color(.systemGreen))
}
.padding()
}
Spacer()
.frame(width: 600, height: 600)
}
}
struct Person: Identifiable {
var id = UUID()
var name: String
}
Now I think I've done something wrong at the append action in the button, because when I run the app no textfields are created on the click of the button. I know it has something to do with the parenthesis (contentsOf: persons)...
What am I not understanding?