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

时间:2019-12-04 20:18:29

标签: ios swift ios13

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

  

介绍:当前上下文

但是当我尝试通过将顶部向底部拖动来关闭屏幕时,视图不会消失。

我还尝试添加一个按钮以使其关闭,但这也不起作用。

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

那么,当使用“当前上下文”作为表示样式时,将顶部视图向下拖动时,如何关闭两个视图?

我正在使用“当前上下文”,因为以前的屏幕永远都不会再显示。并且我不想拖拽这两个,而是想拖拽一个使它们都消失。但是它似乎没有按预期工作。

2 个答案:

答案 0 :(得分:1)

尽管“当前上下文”并非如@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
  4. 连接@IBAction
  5. 运行它以查看结果。

答案 1 :(得分:0)

如果您使用全屏演示(这样就不会发生拖拽),那么根本不需要编写代码就很简单。

enter image description here

请注意,黄色视图控制器在显示时显示为中介,而在关闭时则不显示。