import SwiftUI
struct ContactData {
var name: String
var email: String
var message: String
}
struct ContactFormView: View {
@State private var name = ""
@State private var email = ""
@State private var message = ""
@State private var showingAlert = false
@State private var capturedData: ContactData? = nil
var body: some View {
NavigationView {
Form {
Section(header: Text("Your Info")) {
TextField("Name", text: $name)
.autocapitalization(.words)
.disableAutocorrection(true)
TextField("Email", text: $email)
.keyboardType(.emailAddress)
.autocapitalization(.none)
.disableAutocorrection(true)
}
Section(header: Text("Message")) {
TextField("Message", text: $message)
}
Button("Submit") {
capturedData = ContactData(name: name, email: email, message: message)
showingAlert = true
}
.disabled(name.isEmpty || email.isEmpty || message.isEmpty)
}
.navigationTitle("Simple Contact Form")
.alert("Data Captured", isPresented: $showingAlert) {
Button("OK", role: .cancel) { }
} message: {
if let data = capturedData {
Text("Name: \(data.name)\nEmail: \(data.email)\nMessage: \(data.message)")
}
}
}
}
}
struct ContactFormView_Previews: PreviewProvider {
static var previews: some View {
ContactFormView()
}
}This form uses three text fields to collect user input for name, email, and message. Each field is bound to a @State variable to keep track of what the user types.
When the Submit button is tapped, the app creates a ContactData struct instance that holds the entered data in a clear, organized way. This shows how forms capture structured data, making it easy to use or send elsewhere.
An alert then confirms the data was captured by showing the entered values. The Submit button is disabled until all fields have some text, ensuring complete data.