I am trying to pass a binding from a parent list view into a child detail view. The child detail view contains logic to edit the child. I want these changes to be reflected in the parent list view:
import SwiftUI
struct ParentListView: View {
var body: some View {
NavigationStack {
List {
ForEach(0 ..< 5) { number in
NavigationLink(value: number) {
Text("\(number)")
}
}
}
.navigationDestination(for: Int.self) { number in
ChildDetailView(number: number) //Cannot convert value of type 'Int' to expected argument type 'Binding<Int>'
}
}
}
}
struct ChildDetailView: View {
@Binding var number: Int
var body: some View {
VStack {
Text("\(number)")
Button {
number += 10
} label: {
Text("Add 10")
}
}
}
}
But as you can see, I cannot pass number into ChildDetailView because it expects a binding. I have tried putting $ before number but that doesn't work either. Is there a way to make this work, or am I using the new NavigationStack completly wrong?
Well, actually it is possible, but at first it is needed source of truth, i.e. state with data to bind to, and but in this case Binding will update only source, but not destination. It is more appropriate is to use ObservableObject
view model in such case.
Tested with Xcode 14 / iOS 16
Note: binding do not refresh ChildDetailView
in such case - in can be used only for actions, but source is updated !!
Here is main part:
@State private var numbers = [1, 2, 3, 4, 5] // << data !!
var body: some View {
NavigationStack {
List {
ForEach($numbers) { number in // << binding !!
NavigationLink(value: number) {
Text("\(number.wrappedValue)")
}
}
}
.navigationDestination(for: Binding<Int>.self) { number in // << !!
ChildDetailView(number: number) // << binding !!
}
}
}
and a couple of extensions needed for Binding to transfer via navigation link value.
Complete code on GitHub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With