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?