I have multiple scenes in a NavigationView accessed with NavigationLinks.
I have a struct with nested structs which correspond to the scenes.
At the root scene, I have a state struct, and the nested scenes have binding structs. When I’m three levels down in the navigation stack and I modify the data, the app unexpectedly jumps back to the previous scene. So if I have a TextField, then at each character press, the navigation stack jumps back, making the app unusable.
Is this the expected behaviour?
I created a bug report in case this isn't the expected behaviour: FB9771893
This is the smallest project I could create to reproduce this problem.
My Model:
struct Name {
var first: FirstName
var last: String
}
struct FirstName {
var name: String
var origin: Location
}
struct Location {
var country: String
var city: String
}
My scenes:
struct ContentView: View {
@State var name: Name = Name()
var body: some View {
NavigationView {
NameScene(name: $name)
}
}
}
struct NameScene: View {
@Binding var name: Name
var body: some View {
Form {
NavigationLink {
FirstNameScene(firstName: $name.first)
} label: {
Text("First name")
}
TextField("Last name", text: $name.last)
}
.navigationTitle("Name Scene")
}
}
struct FirstNameScene: View {
@Binding var firstName: FirstName
var body: some View {
Form {
TextField("First name", text: $firstName.name)
NavigationLink {
LocationScene(location: $firstName.origin)
} label: {
Text("Origin")
}
}
.navigationTitle("First Name Scene")
}
}
struct LocationScene: View {
@Binding var location: Location
var body: some View {
Form {
TextField("Country", text: $location.country)
TextField("City", text: $location.city)
}
.navigationTitle("Location Scene")
}
}