@sjb_s @dderg @brebispanique @DTS Engineer
Here is the full code (a modification of @sjb_s 's example) that still DOESN'T work well.
The main reason is that navigationDestination is located in View extension. It is how it is designed in my real app where I have a single navigationDestination for the whole app and all possible routes inside it.
Everything works well in iOS 17.x with this approach, but still doesn't work well in iOS 18.0 Beta 5.
Any ideas?
import SwiftUI
class Item: Identifiable, Hashable {
var name: String
init(name: String) {
self.name = name
}
static func == (lhs: Item, rhs: Item) -> Bool {
lhs.name == rhs.name
}
func hash(into hasher: inout Hasher) {
hasher.combine(name)
}
}
class Model: ObservableObject {
@Published var path = [Item]()
}
struct ContentView: View {
var items = [
Item(name: "Item 1"),
Item(name: "Item 2"),
Item(name: "Item 3"),
]
@StateObject private var model = Model()
var body: some View {
TabView {
Text("Go to Tab 2")
.tabItem { Label("Tab 1", systemImage: "storefront") }
.tag("1")
tab2()
.tabItem { Label("Tab 2", systemImage: "globe") }
.tag("2")
}
}
@ViewBuilder
func tab2() -> some View {
NavigationStack(path: $model.path) {
List(items) { item in
NavigationLink(item.name, value: item)
}
.withAppRouter()
}
}
}
extension View {
func withAppRouter() -> some View {
navigationDestination(for: Item.self) { item in
DetailView(item: item)
}
}
}
struct DetailView: View {
var item: Item
var body: some View {
Text("Details...\(item.name)")
}
}