SwiftUI Toggle calls function on change

February 17, 2021

Setup a SwiftUI Toggle in iOS 13

Pass in property to car (not state)

On body{}.onAppear(perform: { ... }) set the state of the button

Use this state in a Binding to get value

Update the state's value after change

struct CarView: View {
    var car: CarDetails
    @State var hasWheels: Bool = false
    var body: some View {
      Toggle(
        isOn: Binding(
          get: { hasWheels },
          set: { value in
            hasWheels = value
            // call function to send network request
          }
        ),
        label: {
          Text("Does car have wheels?").bold()
        }
      ).onAppear(perform: {
            hasWheels = device.hasWheels
      })
    }