gpt4 book ai didi

ios - 如何使用单个按钮操作关闭所有推送和呈现的屏幕

转载 作者:行者123 更新时间:2023-11-28 07:28:05 25 4
gpt4 key购买 nike

我有 3 个 View Controller 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。在这里,我想通过一次操作同时删除推送和呈现。

最佳答案

您可以使用此函数弹出给定 Controller 的类类型

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

:)

关于ios - 如何使用单个按钮操作关闭所有推送和呈现的屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55821919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com