You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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

Posted on Oct 6, 2022 8:55 AM

Reply
Question marked as Top-ranking reply

Posted on Oct 6, 2022 11:21 AM

Why don't you create a text field for the status and bind it to the status variable like you do for the first name and last name?


The only text fields I see in your code are for a first name and last name. Why would someone ever type "Hello" in those text fields? I don't understand why you want the status to be the person's first or last name.

6 replies
Question marked as Top-ranking reply

Oct 6, 2022 11:21 AM in response to liamcharger

Why don't you create a text field for the status and bind it to the status variable like you do for the first name and last name?


The only text fields I see in your code are for a first name and last name. Why would someone ever type "Hello" in those text fields? I don't understand why you want the status to be the person's first or last name.

Oct 10, 2022 10:31 AM in response to liamcharger


liamcharger wrote:

@MrHoffman

It does for me.


There seems to be a discrepancy somewhere then; some code in another module?




Xcode 14.0.1 macOS Monterey 12.6 on Intel, newly-created Swift Language SwiftUI project, cut-and-pasted code, build fails.


Or I'm completely missing something here in the build settings or such, which is also possible.

Make SwiftUI Form Change a Variable's Value With The User's Input

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.