【问题标题】:Dismiss second ViewController from the third ViewController从第三个 ViewController 中关闭第二个 ViewController
【发布时间】:2017-12-19 07:54:48
【问题描述】:

我试图从 VC c 中解散 VC b,其中 VC c 是一个弹出窗口,并且有一个用于退出的按钮,但它不起作用。 流的结构是

VC a ----presents modally----> VC b ----presents a popover----> VC c

当单击弹出窗口中的按钮时,必须关闭 VC c 和 VC b,以便调用 (VC a)ViewWillAppear。

【问题讨论】:

  • 显示你尝试过的代码
  • 我试过了,但这不起作用:self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
  • 能否分享一下展示VC的代码
  • 确保您看到此question。它是objective-c,但有一个非常详细的答案。另见here

标签: ios swift viewcontroller dismiss


【解决方案1】:

Presenter 或 Coordinator 怎么样。

此实例将初始化所有这些 VC 并呈现它们。 从那里你也可以解雇他们。

【讨论】:

    【解决方案2】:
    `protocol ModalHandler {
        func modalDismissed()
    Class SecondViewController: UIViewController, ModalHandler {
        func modalDismissed() {
            self.dismiss(animated: false, completion: nil)
       }
       func open3rdController() {
           let thirdVC = ThirdViewController(_ )
           thirdVC.delegate = self
           self.present(thirdVC, animated: true, completion: nil)
       }
    class ThirdViewController: UIViewController {
        func dismiss() {
            self.delegate.modalDismissed()
       }
    }
    `
    

    【讨论】:

      【解决方案3】:

      试试这个:

      您可以按如下方式从子视图控制器中关闭您的 presentingViewController

      self.presentingViewController?.dismiss(animated: true, completion: nil)
      

      当你添加一个 ViewController 作为 childViewController

      self.parent?.dismiss(animated: true, completion: nil) 
      

      如果此视图控制器是包含视图控制器的子级(例如导航控制器或标签栏 控制器,)

      weak open var parent: UIViewController? { get }
      

      由此视图控制器或其最近的祖先呈现的视图控制器。

      open var presentedViewController: UIViewController? { get }
      

      呈现此视图控制器(或其最远的祖先)的视图控制器。

      open var presentingViewController: UIViewController? { get }
      

      【讨论】:

      • 如何将 vc B 呈现给 vc C 提供这个
      • 提供更多信息,例如如何从 vc B 展示您的 vc C。
      • 我在 ibaction 中调用了要在 VC b 中执行的故事板 segue 作为 performSegue(withIdentifier: "pop", sender: self)。这个 segue 正在展示 VC 可以弹出窗口
      • 我如何分配 VC 的父值,因为它是一个只能获取的属性。
      • 检查更新的答案`self.presentingViewController?.dismiss(animated: true, completion: nil)`@bakhtiarmasood
      【解决方案4】:

      希望这可行

      // Call inside View controller C    
              self.presentingViewController?.dismissViewControllerAnimated(false, completion: nil)
              self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
      

      【讨论】:

        【解决方案5】:

        首先尝试从自身消除 VC b,而不是显示 VC c 只是为了检查它是否有效,使用:self.dismiss(animated: true, completion: nil) 或 VC b 是否嵌入在导航控制器中,如下所示:self.navigationController?.dismiss(animated: true, completion: nil)

        如果上面的那个可行,我建议你实现委托协议,VC c 将在应该完成的时候将解雇委托给 VC b。您也可以为此使用完成块,其中包含 dismiss 代码。

        【讨论】:

        • 但是 VC c 不涵盖当前上下文,它实际上是一个弹出框,所以如果用户不点击弹出框,但在其他地方,VC c 的弹出框会消失,但不会有 VC b 在它下面显示会与流程相矛盾的
        【解决方案6】:

        如果ViewControllers 有类似的层次结构

        VC a ----表现为self.present(objects, animated: true, completion: nil)模态----> VC b ----表现为self.present(objects, animated: true, completion: nil) popover----> VC c

        VC c 上有一个按钮可以移回 VC a 然后你可以使用:

        self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
        

        【讨论】:

        • 它不起作用我已经尝试了数千次​​pan>
        • 结果如何,会发生什么?
        • 能否请您展示您的代码,展示您如何呈现 VC b 和 VC c
        • VC b 显示为 performSegue(withIdentifier: "rewards", sender: nil),VC c 显示为 performSegue(withIdentifier: "pop", sender: self)
        • 请检查按钮@IBAction是否被调用。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多