下面是两个SwiftUI视图的示例,它们的意外行为是关闭窗口而不是解散DetailView:
struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink(destination: DetailView()) {
Text("Go to detail view")
}
}
}
}
struct DetailView: View {
@Environment(.presentationMode) var presentationMode: Binding<PresentationMode>
var body: some View {
Button(action: { presentationMode.wrappedValue.dismiss() }) {
Text("Dismiss")
}
}
}
这是显示这种行为的视频。
你也可以使用NavigationSplitView让你知道NavigationView现在不支持了
struct ContentView: View {
@State private var isDetailViewPresented = false
var body: some View {
NavigationSplitView {
Button("Go to detail view") {
isDetailViewPresented = true
}
} detail: {
if isDetailViewPresented {
DetailView(isPresented: $isDetailViewPresented)
}
}
}
}
struct DetailView: View {
@Binding var isPresented: Bool
var body: some View {
VStack {
Text("Detail View")
Button(action: { isPresented.toggle()
}) {
Text("Dismiss")
}
}
}
}