使用当前上下文作为演示样式时一起关闭多个 ViewController

Dismiss multiple ViewControllers together when using current context as presentation style

我正在创建一个应用程序,我将一个视图控制器放在另一个视图控制器之上

Presentation: current context

但是当我试图通过将顶部拖向底部来关闭屏幕时,视图并没有消失。

我也试过添加一个按钮来关闭它,但这也不起作用。

@IBAction func dis(_ sender: Any) {
    navigationController?.popViewController(animated: true)
    self.dismiss(animated: true)
}

那么,当使用 "current context" 作为演示样式时,如何在向下拖动顶部视图时关闭这两个视图?

我正在使用 "current context",因为以前的屏幕不会再显示了。而不是拖下两个,我想只拖下一个让他们都消失。但它似乎没有按预期工作。

虽然“当前上下文”不是@matt 提到的这个目的,

在这种情况下,您应该解雇提出这个的控制器以同时解雇两者:

self.presentingViewController!.dismiss(animated: true) // `!` is to makeing sure it's not crashing.

演示:

使用这个简单的代码进行演示:

class ViewController: UIViewController {
    @IBAction func present() {
        let destination = storyboard!.instantiateInitialViewController()!
        if presentingViewController != nil {
            // You are not in the A
            if presentingViewController?.presentingViewController != nil {
                // You are in the C
                presentingViewController?.presentingViewController?.dismiss(animated: true)
                return
            } else {
                // You are in the B
                destination.modalPresentationStyle = .currentContext
            }
        }
        present(destination, animated: true, completion: nil)
    }
}

用法:

  1. 创建单视图应用程序
  2. 将一个按钮拖到视图控制器中
  3. 用提供的代码替换 ViewController class
  4. 连接 @IBAction
  5. 运行 看看效果吧

如果您使用全屏显示(这样就没有拖拽关闭),那么基本上不需要代码就可以很简单地做到这一点。

请注意,黄色视图控制器在呈现时显示为中介,但在关闭时不会显示。