Persisting navigation path seems to get these to work as expected:
import SwiftUI
class NavigationModel: ObservableObject {
@Published var presentedItems: [Int] = []
}
struct SidebarEntry: Identifiable, Hashable {
let id = UUID()
var localizedName: String
}
let sidebarEntries = [
SidebarEntry(localizedName: "Files"),
SidebarEntry(localizedName: "Bookmarks"),
]
// MARK: - main -
@main
struct NewNavigationTestApp: App {
@State private var sidebarSelection: SidebarEntry?
@StateObject var navigationModel = NavigationModel()
var body: some Scene {
WindowGroup {
NavigationSplitView {
List(sidebarEntries, selection: $sidebarSelection) { entry in
NavigationLink("\(entry.localizedName)", value: entry)
}
} detail: {
ZStack { // workaround
if let sidebarSelection {
if sidebarSelection.localizedName == "Files" {
TestStackView()
} else if sidebarSelection.localizedName == "Bookmarks" {
Text("Bookmarks View")
} else {
Text("Unknown View")
}
} else {
Text("No selection")
}
}
}
.environmentObject(navigationModel)
}
}
}
// MARK: - FilesView -
struct TestStackView: View {
@EnvironmentObject var navigationModel: NavigationModel
var body: some View {
NavigationStack(path: $navigationModel.presentedItems) {
List(0 ..< 999) { idx in
NavigationLink("\(idx)", value: idx)
}
.navigationTitle("Numbers")
.navigationDestination(for: Int.self) { idx in
Text("Hello \(idx)")
}
}
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: