如何在 swift ios 中关闭 2 个视图控制器? [关闭]

2023-12-20

如何在 Swift iOS 中关闭 2 个视图控制器?

下面是我的代码。

@IBAction func backButtonTapped(sender: AnyObject) {
    self.presentingViewController
        .presentingViewController
        .dismissViewControllerAnimated(true, completion: nil)
}

斯威夫特 3+ 版本。您可以使用下面的代码在 Swift 3 中一次关闭两个视图控制器。

func dismissTwoViews() {
    self.presentingViewController?
        .presentingViewController?.dismiss(animated: true, completion: nil)
}

斯威夫特 4+ 版本。只是我们需要弹出特定的视图控制器使用这个扩展

extension UINavigationController {

func popToViewController(ofClass: AnyClass, animated: Bool = true) {
    if let vc = viewControllers.filter({$0.isKind(of: ofClass)}).last {
        popToViewController(vc, animated: animated)
    }
}

func popViewControllers(viewsToPop: Int, animated: Bool = true) {
    if viewControllers.count > viewsToPop {
        let vc = viewControllers[viewControllers.count - viewsToPop - 1]
        popToViewController(vc, animated: animated)
    }
}

}

并在视图控制器类中像这样使用

for controller in self.navigationController!.viewControllers as 
    Array {
                  if controller.isKind(of: 
    yourPopControllerName.self) {

   self.navigationController?.isNavigationBarHidden = false
                                _ =  
self.navigationController!.popToViewController(controller, 
 animated: false)
                                    break
                                }
                            }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 swift ios 中关闭 2 个视图控制器? [关闭] 的相关文章

随机推荐