Author: saqibkhan

  • SwiftUI Lists & Forms Section

    Section

    Group rows in lists and inputs in forms using Section with optional headers and footers.


    Example: Sections in List

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct SectionListDemo: View {
      var body: some View {
    
    List {
      Section(header: Text("Fruits"), footer: Text("End Fruits")) {
        Text("Apple"); Text("Banana")
      }
      Section(header: Text("Veggies")) { Text("Carrot") }
    }
    } }


    Example: Sections in Form

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct SectionFormDemo: View {
      @State private var name = ""
      @State private var notifications = true
      var body: some View {
    
    Form {
      Section(header: Text("Profile"), footer: Text("Shown to others")) {
        TextField("Name", text: $name)
      }
      Section(header: Text("Preferences")) {
        Toggle("Notifications", isOn: $notifications)
      }
    }
    } }
  • SwiftUI Lists & Forms ForEach

    ForEach

    Iterate over collections to generate views dynamically using ForEach.


    ForEach

    Render a list of items using ForEach.

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct ForEachDemo: View {
      let nums = [1,2,3]
      var body: some View {
    
    VStack {
      ForEach(nums, id: \.self) { n in
        Text("Item \(n)")
      }
    }
    } }
  • SwiftUI Lists & Forms List

    List

    Render collections with List using identifiable items.


    Basic List

    Render a list of items using List and ForEach.

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct Item: Identifiable { let id: Int; let title: String }
    
    struct ListBasicDemo: View {
      let items = [Item(id: 1, title: "Milk"), Item(id: 2, title: "Bread")]
      var body: some View { List(items) { Text($0.title) } }
    }

    The example above shows a list of items using List and ForEach.



    Custom rows with ForEach

    Render a list of items using List and ForEach.

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct Product: Identifiable { let id: Int; let name: String; let price: Double }
    
    struct ListCustomRowsDemo: View {
      let items = [
    
    Product(id: 1, name: "Coffee", price: 2.99),
    Product(id: 2, name: "Tea", price: 1.99)
    ] var body: some View {
    List {
      ForEach(items) { p in
        HStack { Text(p.name); Spacer(); Text(String(format: "$%.2f", p.price)) }
      }
    }
    } }
  • SwiftUI Lists & Forms

    SwiftUI Lists & Forms

    Build data-driven lists with List and collect inputs with Form, rows, and controls.


    Lists

    Render collections with List and ForEach using identifiable items.

    Syntax:

    • List(items) { Text($0.title) }
    • or List { ForEach(items) { ... } }

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct Todo: Identifiable { let id: Int; let title: String }
    
    struct TodoList: View {
      let items = [Todo(id: 1, title: "Buy milk"), Todo(id: 2, title: "Walk dog")]
      var body: some View {
    
    List(items) { t in Text(t.title) }
    } }

    This example displays a simple list of identifiable items using List.



    Forms

    Group input controls using Form and built-in rows.

    Syntax: Form { Section { TextField(...) } Toggle(...) Stepper(...) }

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct SettingsForm: View {
      @State private var name = ""
      @State private var notifications = true
      @State private var count = 1
      var body: some View {
    
    Form {
      Section(header: Text("Profile")) {
        TextField("Name", text: $name)
      }
      Section(header: Text("Preferences")) {
        Toggle("Notifications", isOn: $notifications)
        Stepper("Count: \(count)", value: $count, in: 1...10)
      }
    }
    } }
  • SwiftUI Data Flow

    Environment (Values)


    SwiftUI Data Flow @Environment (Values)

    Read system- or app-provided values like color scheme and locale using @Environment.


    Read environment values (color scheme)

    Access the current color scheme from the environment and display it in the UI.

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct EnvironmentDemo: View {
      @Environment(\.colorScheme) var scheme
      var body: some View {
    
    Text(scheme == .dark ? "Scheme: Dark" : "Scheme: Light")
      .padding()
    } }

    The example above shows a shared object from an ancestor using .environmentObject and consume it in descendants via @EnvironmentObject.

  • SwiftUI Data Flow @EnvironmentObject

    @EnvironmentObject

    Provide shared app-wide state from an ancestor view and consume it with @EnvironmentObject.


    Share app-wide state with @EnvironmentObject

    Provide a shared object from an ancestor using .environmentObject and consume it in descendants via @EnvironmentObject.

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    final class AppSettings: ObservableObject { @Published var theme = "Light" }
    
    struct Root: View {
      @StateObject private var settings = AppSettings()
      var body: some View {
    
    VStack(spacing: 8) {
      Button("Toggle") { settings.theme = (settings.theme == "Light") ? "Dark" : "Light" }
      Child()
    }
    .environmentObject(settings)
    .padding()
    } } struct Child: View { @EnvironmentObject var settings: AppSettings var body: some View { Text("Theme: \(settings.theme)") } }
  • SwiftUI Data Flow @Binding

    @Binding

    Use @Binding to pass a reference to a parent’s state into a child view so updates flow back.


    Bind child input to parent state

    Bind a parent’s state to a child input so edits update the parent value in real time.

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct Child: View {
      @Binding var text: String
      var body: some View {
    
    TextField("Enter", text: $text)
      .textFieldStyle(.roundedBorder)
    } } struct Parent: View { @State private var name = "Kai" var body: some View {
    VStack {
      Text(name)
      Child(text: $name)
    }
    .padding()
    } }
  • SwiftUI Data Flow @State

    @State

    @State is a property wrapper that stores a value locally in a view and updates the UI when the value changes.

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct CounterView: View {
      @State private var count = 0
      var body: some View {
    
    VStack(spacing: 12) {
      Text("Count: \(count)")
      Button("Increment") { count += 1 }
    }
    .padding()
    } }
  • SwiftUI Data Flow

    SwiftUI Data Flow

    Manage state with @State and @Binding, and share data using ObservableObject, @StateObject, and @EnvironmentObject.


    @State and @Binding

    Use @State for local view state and @Binding to expose it to child views.

    Syntax:

    • @State private var name = ""
    • struct Child { @Binding var name: String }
    • pass with Child(name: $name)

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct Child: View {
      @Binding var text: String
      var body: some View {
    
    TextField("Enter", text: $text)
      .textFieldStyle(.roundedBorder)
    } } struct Parent: View { @State private var text = "Hello" var body: some View {
    VStack {
      Text(text)
      Child(text: $text)
    }
    .padding()
    } }

    This example shows a parent exposing local state to a child with @Binding so edits update the parent’s value.



    ObservableObject and StateObject

    Share data across views with ObservableObject.

    Own it with @StateObject or consume with @ObservedObject.

    Syntax:

    • class VM: ObservableObject { @Published var count = 0 }
    • @StateObject private var vm = VM()
    • @ObservedObject var vm: VM

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    class CounterModel: ObservableObject {
      @Published var count = 0
      func increment() { count += 1 }
    }
    
    struct ChildView: View {
      @ObservedObject var model: CounterModel
      var body: some View {
    
    HStack {
      Text("Count: \(model.count)")
      Button("Inc") { model.increment() }
    }
    } } struct ParentView: View { @StateObject private var model = CounterModel() var body: some View {
    VStack(spacing: 12) {
      ChildView(model: model)
    }
    .padding()
    } }

    The parent owns the model with @StateObject, while the child observes it with @ObservedObject to reflect changes.


    @EnvironmentObject

    Provide shared app state from a root view using .environmentObject(), then consume it in descendants with @EnvironmentObject.

    Syntax:

    • Root().environmentObject(model)
    • @EnvironmentObject var model: Model

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    final class AppSettings: ObservableObject {
      @Published var theme = "Light"
    }
    
    struct Root: View {
      @StateObject private var settings = AppSettings()
      var body: some View {
    
    VStack(spacing: 8) {
      Button("Toggle Theme") { settings.theme = (settings.theme == "Light") ? "Dark" : "Light" }
      Child()
    }
    .environmentObject(settings)
    .padding()
    } } struct Child: View { @EnvironmentObject var settings: AppSettings var body: some View { Text("Theme: \(settings.theme)") } }
  • SwiftUI Data Flow

    SwiftUI Data Flow

    Manage state with @State and @Binding, and share data using ObservableObject, @StateObject, and @EnvironmentObject.


    @State and @Binding

    Use @State for local view state and @Binding to expose it to child views.

    Syntax:

    • @State private var name = ""
    • struct Child { @Binding var name: String }
    • pass with Child(name: $name)

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct Child: View {
      @Binding var text: String
      var body: some View {
    
    TextField("Enter", text: $text)
      .textFieldStyle(.roundedBorder)
    } } struct Parent: View { @State private var text = "Hello" var body: some View {
    VStack {
      Text(text)
      Child(text: $text)
    }
    .padding()
    } }

    This example shows a parent exposing local state to a child with @Binding so edits update the parent’s value.



    ObservableObject and StateObject

    Share data across views with ObservableObject.

    Own it with @StateObject or consume with @ObservedObject.

    Syntax:

    • class VM: ObservableObject { @Published var count = 0 }
    • @StateObject private var vm = VM()
    • @ObservedObject var vm: VM

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    class CounterModel: ObservableObject {
      @Published var count = 0
      func increment() { count += 1 }
    }
    
    struct ChildView: View {
      @ObservedObject var model: CounterModel
      var body: some View {
    
    HStack {
      Text("Count: \(model.count)")
      Button("Inc") { model.increment() }
    }
    } } struct ParentView: View { @StateObject private var model = CounterModel() var body: some View {
    VStack(spacing: 12) {
      ChildView(model: model)
    }
    .padding()
    } }

    The parent owns the model with @StateObject, while the child observes it with @ObservedObject to reflect changes.


    @EnvironmentObject

    Provide shared app state from a root view using .environmentObject(), then consume it in descendants with @EnvironmentObject.

    Syntax:

    • Root().environmentObject(model)
    • @EnvironmentObject var model: Model

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    final class AppSettings: ObservableObject {
      @Published var theme = "Light"
    }
    
    struct Root: View {
      @StateObject private var settings = AppSettings()
      var body: some View {
    
    VStack(spacing: 8) {
      Button("Toggle Theme") { settings.theme = (settings.theme == "Light") ? "Dark" : "Light" }
      Child()
    }
    .environmentObject(settings)
    .padding()
    } } struct Child: View { @EnvironmentObject var settings: AppSettings var body: some View { Text("Theme: \(settings.theme)") } }

    The root injects a shared AppSettings; any descendant can read updates via @EnvironmentObject.