import SwiftUI
struct ChildView: View {
let entryLabel : String
@Binding var date : Date?
var body: some View {
HStack(spacing:20) {
Text(entryLabel)
.font(.title3)
Spacer()
Button("Add Date") {
date = Date.now
print("add \(date)")
}
.buttonStyle(.bordered)
}
.padding(.horizontal,5)
.padding(.vertical,10)
}
}
struct ChildDateView_Previews: PreviewProvider {
static var previews: some View {
@State var date : Date?
VStack {
ChildView(entryLabel: "The label", date: $date)
let s = date?.formatted() ?? "No Date"
Text(s)
}
.onChange(of: date) { newValue in
print("changed")
}
}
}
Above simple view, idea is that pressing the button will set the binding date
with a value of the current date. But the binding value never changes from nil. Only get "add nil" printed in the console. And of course the onChange
never fires. Guessing it has to be something stupid on my part but I simply cannot see it. What am I doing wrong here or not getting?