ios - 关闭模式视图时未调用 onDisappear
问题描述
我依靠 SwiftUI.onDisappear
来执行一些逻辑,但是当用户使用滑动手势关闭模态呈现的视图时,它不会被调用。重现
- 以“ChildView 1”形式呈现视图
- 在此视图中,将“ChildView 2”推送为导航子项
- 向下滑动以关闭模式视图。
未调用“ChildView 2”的 .onDisappear。
重现的示例代码
import SwiftUI
struct ContentView: View {
@State var isShowingModal
var body: some View {
NavigationView {
Button(action: {
self.isShowingModal.toggle()
}) {
Text("Show Modal")
}
}
.sheet(isPresented: $isShowingModal) {
NavigationView {
ChildView(title: 1)
}
}
}
}
struct ChildView: View {
let title: Int
var body: some View {
NavigationLink(destination: ChildView(title: title + 1)) {
Text("Show Child")
}
.navigationBarTitle("View \(title)")
.onAppear {
print("onAppear ChildView \(self.title)")
}
.onDisappear {
print("onDisappear ChildView \(self.title)")
}
}
}
输出是:
onAppear ChildView 1
onAppear ChildView 2
onDisappear ChildView 1
解决方案
如果您正在寻找在实际模式被解除时发生的逻辑,您将要在这里调用它,我打印出 Modal Dismissed:
struct ContentView: View {
@State var isShowingModal = false
var body: some View {
NavigationView {
Button(action: {
self.isShowingModal.toggle()
}) {
Text("Show Modal")
}
}
.sheet(isPresented: $isShowingModal) {
NavigationView {
ChildView(title: 1)
}
.onDisappear {
print("Modal Dismissed")
}
}
}
}
推荐阅读
- c - 这个基于指针的 strcat 在 C 中是如何工作的?
- python-3.x - 使用递归和默认值根据输入绘制模式
- snowflake-cloud-data-platform - 如何根据雪花中的返回值提交/回滚存储过程?
- ggplot2 - Getting the zoom ratio in ggsave with transparent background
- spring-boot - Netbeans 11,如何从 Springboot 应用程序生成战争文件
- excel - 需要比较两列并用一张表中的数据完成第三列
- javascript - 请帮助我在 javascript 中构建 reg ex
- node.js - 如何获取服务器的成员数?
- api - 无法访问在应用程序脚本中制作的 API 内容
- python - 如果 GridSearchCV 给出了一些排名为 1 的估计器,它将选择哪一个作为最佳估计器?