kristofk
(Kristof Kocsis)
1
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")
}
}
malhal
(Malcolm Hall)
2
Hi, this is not a bug, there are 2 ways to fix your problem:
-
.isDetailLink(false) on the NavigationLink in NameScene.
-
.navigationViewStyle(.stack) on the NavigationView.
struct Name {
var first: FirstName = FirstName()
var last: String = ""
}
struct FirstName {
var name: String = ""
var origin: Location = Location()
}
struct Location {
var country: String = ""
var city: String = ""
}
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")
}
.isDetailLink(false) // the fix
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")
}
}
1 Like
kristofk
(Kristof Kocsis)
3
I was about to check out your fix but I ran this project again today and I couldn't reproduce the bug. I don't know what happened maybe something received an update but my problem seems to have fixed itself... 
Thank you for your response tho. 