Day 62 of 100 Days Of SwiftUI
Oluwasanmi Aderibigbe
Posted on May 15, 2021
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)
}
}
}
This code creates a custom binding that sets the value of the bidAmount
state property and also prints the value of bidAmount
💖 💪 🙅 🚩
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.