从第三个ViewController中关闭第二个ViewController

问题描述 投票:1回答:6

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

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

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

ios swift viewcontroller dismiss
6个回答
2
投票

试试这个:

您可以从子视图控制器中解除您的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 }

0
投票

如果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,然后您可以使用:

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

0
投票

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

如果上面的那个工作,我建议你实现委托协议,VC c将委托给VC解雇,只要它应该完成。您也可以使用完成块,包含dismiss代码。


0
投票

希望这有效

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

0
投票
`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()
   }
}
`

0
投票

什么是关于PresenterCoordinator

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

© www.soinside.com 2019 - 2024. All rights reserved.