Make SwiftUI Form Change a Variable's Value With The User's Input
How can I make the fields in a form add the users input to a string variable?
The code is below:
import SwiftUI
struct ContentView: View {
var colorPicker = ["Select an option", "Red", "Brown", "Tan"]
var oppoAttractPicker = ["Select an option", "Yes", "No",]
var intExtPicker = ["Select an option", "Intravert", "Extravert"]
@State var firstName = ""
@State var lastName = ""
@State var selectedIntExt = "Select an option"
@State var selectedOppoAttract = "Select an option"
@State var selectedColor = "Select an option"
@State var status = ""
var body: some View {
NavigationView {
VStack {
Form {
Section {
TextField("First Name", text: $firstName)
TextField("Last Name", text: $lastName)
}
Section {
Picker("Opposites attract", selection: $selectedOppoAttract) {
ForEach(oppoAttractPicker, id: \.self) {
Text($0)
}
}
Picker("Choose color", selection: $selectedColor) {
ForEach(colorPicker, id: \.self) {
Text($0)
}
}
Picker("Intravert/Extravert", selection: $selectedIntExt) {
ForEach(intExtPicker, id: \.self) {
Text($0)
}
}
}
}
}
.navigationTitle("My App")
.toolbar {
NavigationLink(destination: SaveView()) {
HStack {
Text("Done")
}
}
}
}
}
}
struct ContentView_Preview: PreviewProvider {
static var previews: some View {
ContentView()
}
}
So if the user enters "Hello" in the one of the text fields, "Hello" will be saved in the status var.
Thanks,
Liam
MacBook Pro Apple Silicon