[英]onDisappear not called when a modal View is dismissed
I rely on the SwiftUI's .onDisappear
to perform some logic but it is not being called when the user dismisses a modally presented view with the swipe gesture.我依靠 SwiftUI 的.onDisappear
来执行一些逻辑,但是当用户使用滑动手势关闭模态呈现的视图时,它不会被调用。 To reproduce繁殖
The .onDisappear of "ChildView 2" is not called.未调用“ChildView 2”的 .onDisappear。
Sample code to reproduce重现的示例代码
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)")
}
}
}
The output is:输出是:
onAppear ChildView 1
onAppear ChildView 2
onDisappear ChildView 1
If you're looking for logic to occur when the actual modal is dismissed, you're going to want to call that here, where I print out Modal Dismissed:如果您正在寻找在实际模态被解除时发生的逻辑,您将要在此处调用它,在那里我打印出 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")
}
}
}
}
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)
}
}
}
}
in this code, you have NavigationView, and when presenting sheet, you push there another NavigationView.在此代码中,您有 NavigationView,并且在呈现工作表时,您将另一个 NavigationView 推到那里。 This is the source of trouble这是麻烦的根源
You don't need any NavigationView to present modals.您不需要任何 NavigationView 来呈现模态。 If you like to present another modal from modal, you can use如果你想从模态中呈现另一个模态,你可以使用
import SwiftUI
struct ContentView: View {
var body: some View {
ChildView(title: 1)
}
}
struct ChildView: View {
@State var isShowingModal = false
let title: Int
var body: some View {
Button(action: {
self.isShowingModal.toggle()
}) {
Text("Show Modal \(title)").font(.largeTitle)
}
.sheet(isPresented: $isShowingModal) {
ChildView(title: self.title + 1)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
UPDATE更新
from Apple Human Interface Guidelines来自 Apple人机界面指南
Modality is a design technique that presents content in a temporary mode that's separate from the user's previous current context and requires an explicit action to exit模态是一种设计技术,它以一种与用户之前的当前上下文分开的临时模式呈现内容,并且需要明确的操作才能退出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.