DEV Community

Oluwasanmi Aderibigbe
Oluwasanmi Aderibigbe

Posted on

Day 62 of 100 Days Of SwiftUI

I just completed day 62 of 100 Days Of SwiftUI. Today I learnt about custom bindings and Actionsheet.

Custom bindings are useful when you need to perform side effects for example logging whenever your @State property changes.
For example, this is how you create a custom binding in SwiftUI:

struct ContentView: View {
    @State private var bidAmount: CGFloat = 0

    var body: some View {
        let bid = Binding<CGFloat>(
            get: {
                self.bidAmount
            },
            set: {
                self.bidAmount = $0
                print("New value is \(self.bidAmount)")
            }
        )

        return VStack {
            Text("Bid amount: \(bidAmount)")

            Slider(value: bid, in: 0...20)
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

This code creates a custom binding that sets the value of the bidAmount state property and also prints the value of bidAmount

Top comments (0)