DEV Community

Oluwasanmi Aderibigbe
Oluwasanmi Aderibigbe

Posted on

2 1

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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay