最小的可重现示例(Xcode 11.2 beta,在Xcode 11.1中有效):
struct Parent: View {
var body: some View {
NavigationView {
Text("Hello World")
.navigationBarItems(
trailing: NavigationLink(destination: Child(), label: { Text("Next") })
)
}
}
}
struct Child: View {
@Environment(\.presentationMode) var presentation
var body: some View {
Text("Hello, World!")
.navigationBarItems(
leading: Button(
action: {
self.presentation.wrappedValue.dismiss()
},
label: { Text("Back") }
)
)
}
}
struct ContentView: View {
var body: some View {
Parent()
}
}
Run Code Online (Sandbox Code Playgroud)
问题似乎在于将我放置NavigationLink在navigationBarItems嵌套于SwiftUI视图(其根视图为)的修改器内部NavigationView。崩溃报告表明,当我向前导航Child然后返回时,我试图弹出一个不存在的视图控制器Parent …