如何通过单键操作关闭所有推送和显示的屏幕

时间:2019-04-24 03:20:10

标签: ios swift dismiss presentviewcontroller

我有3个Viewcontrollers HomePageControllerChangePasswordViewControllerPasswordChangedDoneController

在主页更改密码按钮操作中,我将屏幕推入ChangePasswordViewController

let vc = self.storyboard?.instantiateViewController(withIdentifier: "ChangePasswordViewController")as! ChangePasswordViewController
self.navigationController?.pushViewController(vc, animated: true)

现在我在ChangePasswordViewControllerPasswordChangedDoneController屏幕上显示。

let vc = self.storyboard?.instantiateViewController(withIdentifier: "PasswordChangedController")as! PasswordChangedDoneController
self.navigationController?.present(vc, animated: true, completion: nil)

现在,我想使用HomePageController中的Gotit Button Action转到PasswordChangedDoneController。 在这里,我想一次删除一次推送和展示。

1 个答案:

答案 0 :(得分:0)

您可以使用此功能弹出给定控制器的类类型

extension UIViewController {

    /// like UIViewController.popToRoot
    public func popToRoot() {
        guard let navigationController = navigationController else { return  dismiss(animated: true) }
        navigationController.popToRootViewController(animated: true)
    }

    /// like UIViewController.popTo(:)
    public func popTo(_ vc: UIViewController.Type, _ orPopToRoot: Bool = true) {
        guard let navigationController = navigationController else { return popToRoot() }
        let list = navigationController.viewControllers.reversed().filter { return $0.isKind(of: vc) }
        guard let c = list.first else {
            if orPopToRoot { self.popToRoot() }
            else { self.popSelf(animated: true) }
            return
        }
        navigationController.popToViewController(c, animated: true)
    }

    /// like UIViewController.popViewController
    ///
    /// - Note: if can't found navigationController. will 'dismiss(animated:completion:)'
    public func popSelf(animated: Bool) {
        guard let navigationController = navigationController else { return  dismissSelf(animated: animated) }
        navigationController.popViewController(animated: animated)
    }

    /// like UIViewController.dismiss
    public func dismissSelf(animated: Bool, completion: (() -> Void)? = nil) {
        dismiss(animated: animated, completion: completion)
    }
}

:)