SwiftUI Codes
Best SwiftUI Code Warehouse

Lesson 8: Forms and User Inputs

Objective:

Learn how to create forms and handle user inputs.

Content:

1. TextField and SecureField

Using TextField and SecureField for text input.

Text Input:

@State private var username = ""
@State private var password = ""

var body: some View {
    Form {
        TextField("Username", text: $username)
        SecureField("Password", text: $password)
    }
}

Explanation:

Handling Input Changes:

@State private var email = ""

var body: some View {
    VStack {
        TextField("Enter your email", text: $email)
            .padding()
            .border(Color.gray, width: 1)
        Text("Email: \(email)")
    }
}

Explanation:

2. TextEditor

Using TextEditor for multi-line text input.

Multi-line Text Input:

@State private var bio = "Enter your bio..."

var body: some View {
    TextEditor(text: $bio)
        .frame(height: 150)
        .padding()
        .border(Color.gray, width: 1)
}

Explanation:

3. Other Input Components

Using Toggle, Slider, and Stepper for different types of user inputs.

Toggle, Slider, Stepper:

@State private var isOn = false
@State private var volume = 0.5
@State private var count = 0

var body: some View {
    Form {
        Toggle("Enable Feature", isOn: $isOn)
        Slider(value: $volume, in: 0...1)
        Stepper("Count: \(count)", value: $count)
    }
}

Explanation:

Example Form:

@State private var name = ""
@State private var age = 18

var body: some View {
    Form {
        Section(header: Text("Personal Information")) {
            TextField("Name", text: $name)
            Stepper("Age: \(age)", value: $age, in: 0...100)
        }

        Section {
            Button("Submit") {
                print("Name: \(name), Age: \(age)")
            }
        }
    }
}

Explanation:

4. Form Validation and Error Handling

Implementing validation logic to handle user input errors.

Validating Inputs:

@State private var email = ""
@State private var showError = false

var body: some View {
    Form {
        TextField("Email", text: $email)
            .autocapitalization(.none)
            .keyboardType(.emailAddress)

        if showError {
            Text("Invalid email address")
                .foregroundColor(.red)
        }

        Button("Submit") {
            if isValidEmail(email) {
                showError = false
                // Submit form
            } else {
                showError = true
            }
        }
    }
}

func isValidEmail(_ email: String) -> Bool {
    // Simple email validation logic
    return email.contains("@") && email.contains(".")
}

Explanation:

Advanced Validation:

@State private var password = ""
@State private var confirmPassword = ""
@State private var showPasswordError = false

var body: some View {
    Form {
        SecureField("Password", text: $password)
        SecureField("Confirm Password", text: $confirmPassword)

        if showPasswordError {
            Text("Passwords do not match")
                .foregroundColor(.red)
        }

        Button("Submit") {
            if password == confirmPassword {
                showPasswordError = false
                // Submit form
            } else {
                showPasswordError = true
            }
        }
    }
}

Explanation:

Lesson 9: Animations with SwiftUI