I think I have the same issue with the following view.
Use the sidebar to navigate to "Item home"
Use a navigation link to go to "Nav Test 1"
Use the sidebar to navigate to "Item market"
Use a navigation link to go to "Nav Test 2"
Use the sidebar to navigate back to "Item home"
Note the path is not the same as when you navigated away from the original selection in step 3.
import SwiftUI
struct NavBug1: View {
@State var selection: Item?
@State var paths: [Item: NavigationPath] = [:]
var body: some View {
NavigationSplitView {
List(Item.allCases, selection: $selection) { item in
Text("Item \(item.rawValue)")
}
} detail: {
if let selection {
NavigationStack(path: path(for: selection)) {
List {
Text("Item \(selection.rawValue)")
NavigationLink("Nav Test 1", value: Nav1.test1)
NavigationLink("Nav Test 2", value: Nav1.test2)
}
.navigationTitle("Item \(selection.rawValue)")
.navigationDestination(for: Nav1.self) { nav1 in
Text("Nav Item \(nav1.rawValue)")
.navigationTitle("Nav Item \(nav1.rawValue)")
}
}
}
}
}
func path(for item: Item?) -> Binding<NavigationPath> {
Binding {
if let selection, let path = paths[selection] {
path
} else {
.init()
}
} set: { newValue in
if let selection {
paths[selection] = newValue
}
}
}
enum Item: String, Equatable, Hashable, Identifiable, CaseIterable {
case home
case market
var id: Self { self }
}
enum Nav1: String, Equatable, Hashable, Identifiable, CaseIterable {
case test1
case test2
var id: Self { self }
}
}