简体   繁体   English

关闭模态视图时不调用 onDisappear

[英]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繁殖

  • Present a view modally a "ChildView 1"以模态方式呈现视图“ChildView 1”
  • On this view, push a "ChildView 2" as a navigation child在此视图上,推送“ChildView 2”作为导航子项
  • Swipe down to dismiss the modal view.向下滑动以关闭模态视图。

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.

相关问题 解除模态视图时未调用ViewDidAppear - ViewDidAppear not called when the modal view is dismissed 取消模态视图时VC中的触发功能 - Trigger function in VC when modal view is dismissed 关闭B视图时未调用UITableView函数 - A UITableView functions not called when B view is dismissed 模态视图控制器被关闭后如何调用函数 - How to call a function when a Modal View Controller has been dismissed 关闭时如何将数据从模态视图控制器传回 - How to pass data from modal view controller back when dismissed 模态视图关闭时如何获取精确的PresentingViewController - How to get exact PresentingViewController when Modal View dismissed iOS - 了解模态视图何时被解除 - iOS - Know when modal view has been dismissed 为什么简单的模态视图控制器在显示和关闭时会滞后? - Why would a simple modal view controller lag when presented and dismissed? 父视图 controller 中是否有在模态视图被关闭后被调用的委托? - Is there a delegate in the parent view controller that gets called after a modal view gets dismissed? TabBarController viewDidAppear 在呈现的视图控制器关闭时未调用 - TabBarController viewDidAppear not called when presented view controller dismissed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM