Thank you for the explanation at [https://developer.apple.com/forums/thread/758371].
As you understand, I am using navigationDestination(isPresented
) with get/set binding because navigationDestination(item:) is only supported from iOS 17 onwards. Our product code supports iOS 16 and later. (The product itself also supports iOS 15, so we are using NavigationView as well.)
Additionally, when I modified the code provided in the post to be closer to our product code by changing navigationDestination(for:) to navigationDestination(item:), the pop issue reoccurred (during the transition from contentView to subView).
Therefore, I believe this issue is not related to the view-destination or value-destination problem. As a workaround, I have confirmed that commenting out @Environment(.dismiss) defined in ContentView avoids the issue.
While the code in this article is very simple, our product code is enormous and has a very complex structure, making it extremely difficult to pinpoint the cause when behavior changes with or without @Environment.
The following code is automatically popped when navigating from ContentView to SubView on iOS 18 beta 2, but this does not happen on iOS 17.5.
import SwiftUI
struct Selection: Hashable, Identifiable {
var num: Int
var id: Int { num }
}
enum Kind: Hashable {
case a
case b
}
struct RootView3: View {
@State var kind: Kind? = nil
@State var vals: [Selection] = (1...5).map(Selection.init(num:))
@State var selection: Selection?
var body: some View {
if #available(iOS 17.0, *) {
NavigationStack {
List(selection: $kind) {
NavigationLink("Album-a", value: Kind.a)
NavigationLink("Album-b", value: Kind.b)
}
.navigationDestination(item: $kind, destination: { kind in
ContentView3(vals: $vals, selection: $selection)
})
// .navigationDestination(for: Kind.self) { kind in
// ContentView3(vals: $vals, selection: $selection)
// }
}
}
}
}
@available(iOS 17.0, *)
struct ContentView3: View {
@Binding var vals: [Selection]
@Binding var selection: Selection?
@Environment(\.dismiss) private var dismiss
var body: some View {
List(selection: $selection) {
ForEach(self.vals) { val in
NavigationLink(value: val) {
Text("\(String(describing: val))")
}
}
}
.navigationDestination(item: $selection) { sel in
SubView3(kind: .a, selection: sel)
}
}
}
struct SubView3: View {
let kind: Kind
let selection: Selection
var body: some View {
Text("Content. \(kind): \(selection)")
}
}