Day 62 of 100 Days Of SwiftUI

sanmiade

Oluwasanmi Aderibigbe

Posted on May 15, 2021

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

💖 💪 🙅 🚩
sanmiade
Oluwasanmi Aderibigbe

Posted on May 15, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Go from one screen to another in SwiftUI
100daysofcode Go from one screen to another in SwiftUI

March 27, 2023

model show on click in swiftUI
100daysofcode model show on click in swiftUI

March 24, 2023

confirmationDialog() in swiftUI
100daysofcode confirmationDialog() in swiftUI

March 24, 2023

Item delete & Move in SwiftUI
100daysofcode Item delete & Move in SwiftUI

March 24, 2023