有没有办法在视图控制器离开视图堆栈后调用函数?

Is there a way to call a function once a View Controller leaves the view stack?

如果另一个视图控制器位于其上方(例如,您将视图控制器推到它上面),则仍会调用 ViewDidDisapear 和 ViewWillDisappear。是否有仅在视图控制器从导航堆栈中移除后才调用的函数?向后退按钮添加一个功能是可行的,但是如果用户决定使用边缘平移手势来关闭视图怎么办?是否有一个动作可以解释这两个事件?

是的,我能想出几种方法来做到这一点

一种选择是将一些代码添加到 UIViewController 的 dealloc 方法中。

如果您不希望视图控制器控制器在离开堆栈时被释放,您还可以为 UINavigationController 设置一个 UINavigationControllerDelegate 并定义

func navigationController(_ navigationController: UINavigationController,
                               didShow viewController: UIViewController,
                               animated: Bool) {
  guard let poppedViewController =
        navigationController.transitionCoordinator?.viewController(forKey: .from) 
  <Do something with the popped VC>