After modifying the ContentView as shown below, the transition to the SubView now works.
(🌟 indicates the modified parts)
However, since the cause of the issue is unknown, I cannot judge whether this is an appropriate workaround or not.
struct ContentView: View {
@StateObject private var model = ContentModel()
@State private var selectedData: Value?
// 🌟
@State private var isShowingSubView = false
@Environment(\.dismiss) private var dismiss
init() {
}
var body: some View {
List(selection: $selectedData) {
ForEach(model.vals) { val in
NavigationLink(value: val) {
Text("\(val.num)")
}
}
}
// 🌟
.onChange(of: selectedData, perform: { newValue in
// In production code, convert data here.
isShowingSubView = newValue != nil
})
.navigationDestination(isPresented: $isShowingSubView, destination: {
SubView(kind: model.kind)
// SubView()
})
.onChange(of: isShowingSubView) { newValue in
if !newValue && selectedData != nil {
selectedData = nil
}
}
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: