NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

How to create custom bindings

Paul Hudson    @twostraws   

Updated for Xcode 14.2

When we use SwiftUI’s @State property wrapper it does a huge amount of work on our behalf to allow two-way bindings for user interface controls. However, we can also create bindings by hand using the Binding type, which can be provided with custom get and set closures to run when the value is read or written.

For example, this creates a trivial binding that just acts as a passthrough for another property:

struct ContentView: View {
    @State private var username = ""

    var body: some View {
        let binding = Binding(
            get: { self.username },
            set: { self.username = $0 }
        )

        return VStack {
            TextField("Enter your name", text: binding)
        }
    }
}

Download this as an Xcode project

Tip: When binding to a custom Binding instance, you don’t need to use the dollar sign before the binding name – you’re already reading the two-way binding.

Custom bindings are useful when you want to add extra logic to a binding being read or written – you might want to perform some calculations before sending a value back, or you might want to take some extra actions when the value is changed.

For example, we could create a stack of two toggle switches where both can be off and either one can be on, but both can’t be on at the same time – enabling one will always disable the other. Here’s how that looks in code:

struct ContentView: View {
    @State private var firstToggle = false
    @State private var secondToggle = false

    var body: some View {
        let firstBinding = Binding(
            get: { self.firstToggle },
            set: {
                self.firstToggle = $0

                if $0 == true {
                    self.secondToggle = false
                }
            }
        )

        let secondBinding = Binding(
            get: { self.secondToggle },
            set: {
                self.secondToggle = $0

                if $0 == true {
                    self.firstToggle = false
                }
            }
        )

        return VStack {
            Toggle(isOn: firstBinding) {
                Text("First toggle")
            }

            Toggle(isOn: secondBinding) {
                Text("Second toggle")
            }
        }
    }
}

Download this as an Xcode project

Hacking with Swift is sponsored by Stream

SPONSORED Build a functional Twitter clone using APIs and SwiftUI with Stream's 7-part tutorial series. In just four days, learn how to create your own Twitter using Stream Chat, Algolia, 100ms, Mux, and RevenueCat.

Try now!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.9/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.