如何通过单按钮操作关闭所有推送和呈现的屏幕



我有 3 个视图控制器HomePageControllerChangePasswordViewControllerPasswordChangedDoneController

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

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

现在我正在呈现屏幕ChangePasswordViewController PasswordChangedDoneController.

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

现在我想在PasswordChangedDoneController中使用Gotit按钮操作去HomePageController.在这里,我想通过单个操作一次删除推送和呈现。

您可以使用

此函数弹出给定控制器的类类型

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)
    }
}

:)

最新更新