swiftui - 如何从 NavigationLink 中关闭工作表
问题描述
在下面的示例中,我有一个显示一张ViewOne
. ViewOne
有一个NavigationLink
到ViewTwo
。
我怎样才能解除工作表ViewTwo
?
使用presentationMode.wrappedValue.dismiss()
导航回ViewOne
.
struct ContentView: View {
@State private var isShowingSheet = false
var body: some View {
Button("Show sheet", action: {
isShowingSheet.toggle()
})
.sheet(isPresented: $isShowingSheet, content: {
ViewOne()
})
}
}
struct ViewOne: View {
var body: some View {
NavigationView {
NavigationLink("Go to ViewTwo", destination: ViewTwo())
.isDetailLink(false)
}
}
}
struct ViewTwo: View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
Button("Dismiss sheet here") {
presentationMode.wrappedValue.dismiss()
}
}
}
解决方案
这可能取决于平台——NavigationView
例如,在 macOS 上,您现有的代码可以工作。
将 a 显式传递Binding
给原始工作表状态应该可以工作:
struct ContentView: View {
@State private var isShowingSheet = false
var body: some View {
Button("Show sheet", action: {
isShowingSheet.toggle()
})
.sheet(isPresented: $isShowingSheet, content: {
ViewOne(showParentSheet: $isShowingSheet)
})
}
}
struct ViewOne: View {
@Binding var showParentSheet : Bool
var body: some View {
NavigationView {
NavigationLink("Go to ViewTwo", destination: ViewTwo(showParentSheet: $showParentSheet))
//.isDetailLink(false)
}
}
}
struct ViewTwo: View {
@Binding var showParentSheet : Bool
@Environment(\.presentationMode) var presentationMode
var body: some View {
Button("Dismiss sheet here") {
showParentSheet = false
}
}
}
推荐阅读
- google-chrome - Chrome DevTools 打开时阻止页面刷新
- php - PHP MySQL分组问题
- python - 500 内部服务器错误 - Django wsgi 错误显示在 apache2 日志上
- c++ - 找到两对数字,使它们的乘积绝对差最小化
- ios - 可以显示UILabel代码,但不能显示UITextView。我错过了什么?
- python - Python 改变我的绘图数据点?[直方图]
- macos - 如何将 macOS 上的 dylib 与 clang(例如 libjpeg)链接?
- reporting-services - 摘要和个人的 SSRS 报告
- linux - 使用 GCC 内联汇编获取 Linux 系统调用的返回值
- postgresql - 如何将一个表与另一个表中的一系列值连接起来?