如何在Swift中弹出特定的View Controller

2020/12/16 21:52 · ios ·  · 0评论

我使用Objective-C下面代码弹出特定代码ViewController

for (UIViewController *controller in self.navigationController.viewControllers) {
    if ([controller isKindOfClass:[AnOldViewController class]]) { 
        //Do not forget to import AnOldViewController.h
        [self.navigationController popToViewController:controller
                                              animated:YES];
        break;
    }
}

我该如何在Swift中做到这一点?

尝试以下代码:

for controller in self.navigationController!.viewControllers as Array {
    if controller.isKind(of: ViewController.self) {
        self.navigationController!.popToViewController(controller, animated: true)
        break
    }
}

对于Swift 3+

let viewControllers: [UIViewController] = self.navigationController!.viewControllers
for aViewController in viewControllers {
    if aViewController is YourViewController {
        self.navigationController!.popToViewController(aViewController, animated: true)
    }
}

迅捷4.0

for controller in self.navigationController!.viewControllers as Array {
        if controller.isKind(of: DashboardVC.self) {
            _ =  self.navigationController!.popToViewController(controller, animated: true)
            break
        }
    }

这是完美的工作。

迅捷5

弹出特定类的最新实例,例如SomeViewController

navigationController?.popToViewController(ofClass: SomeViewController.self)

但是您需要添加以下UINavigationController扩展名:

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

我更喜欢一种通用的方法。

我有UINavigationController的扩展名:

extension UINavigationController {

   func backToViewController(vc: Any) {
      // iterate to find the type of vc
      for element in viewControllers as Array {
         if "\(element.dynamicType).Type" == "\(vc.dynamicType)" {
            self.popToViewController(element, animated: true)
            break
         }
      }
   }

}

假设我有一个在导航堆栈中实例化的FOHomeVC类(它是一个UIViewController)。

所以我会在我的代码中这样做:

self.navigationController?.backToViewController(FOHomeVC.self)

我添加了一个扩展,UINavigationController可帮助您查找导航堆栈中是否存在该控制器。如果是,则它将弹出到该控制器,否则您将传递新控制器以使用pushController参数进行推送

extension UINavigationController {

    func containsViewController(ofKind kind: AnyClass) -> Bool {
        return self.viewControllers.contains(where: { $0.isKind(of: kind) })
    }

    func popPushToVC(ofKind kind: AnyClass, pushController: UIViewController) {
        if containsViewController(ofKind: kind) {
            for controller in self.viewControllers {
                if controller.isKind(of: kind) {
                    popToViewController(controller, animated: true)
                    break
                }
            }
        } else {
            pushViewController(pushController, animated: true)
        }
    }
}

从导航堆栈中找到视图控制器,然后弹出该视图控制器(如果存在)

for vc in self.navigationController!.viewControllers {
    if let myViewCont = vc as? VCName 
    {
        self.navigationController?.popToViewController(myViewCont, animated: true)
    }
}

迅捷5

let controllers : Array = self.navigationController!.viewControllers
self.navigationController!.popToViewController(controllers[1], animated: true)

迅捷4 /迅捷5

 for controller in self.navigationController!.viewControllers as Array {
            if controller.isKind(of: HomeViewController.self) {
                self.navigationController!.popToViewController(controller, animated: true)
                break
            }
        }

迅速地

   @IBAction func popToConversationsVC(_ sender: UIButton) {
        if (self.navigationController != nil) {
            for vc in  self.navigationController!.viewControllers {
                if vc is ConversationsVC {
                     self.navigationController?.popToViewController(vc, animated: false)
                }
            }
            }
    }

我更喜欢“真正的通用”且功能更多的方法。

所以我想出了以下UINavigationController扩展功能。您还可以将first函数用于其他任何事情,您只需要访问导航堆栈中的特定VC。


扩展名

extension UINavigationController {
    func getViewController<T: UIViewController>(of type: T.Type) -> UIViewController? {
        return self.viewControllers.first(where: { $0 is T })
    }

    func popToViewController<T: UIViewController>(of type: T.Type, animated: Bool) {
        guard let viewController = self.getViewController(of: type) else { return }
        self.popToViewController(viewController, animated: animated)
    }
}

用法

self.navigationController?.popToViewController(of: YourViewController.self, animated: true)


这至少应该在Swift 4和5中有效。

@PabloR的Swift 5答案在这里:

extension UINavigationController {

   func backToViewController(vc: Any) {
      // iterate to find the type of vc
      for element in viewControllers as Array {
        if "\(type(of: element)).Type" == "\(type(of: vc))" {
            self.popToViewController(element, animated: true)
            break
         }
      }
   }

}

用法:

self.navigationController?.backToViewController(vc: TaskListViewController.self)

请针对Swift 3.0使用以下代码:

 let viewControllers: [UIViewController] = self.navigationController!.viewControllers as [UIViewController];

for aViewController:UIViewController in viewControllers {
            if aViewController.isKind(of: YourViewController.self) {
                _ = self.navigationController?.popToViewController(aViewController, animated: true)
            }
        }

我需要使用它,因为在某些情况下应用程序崩溃:

if let navVC = self.navigationController {
                    let views = navVC.viewControllers as Array
                    for controller in views {
                        if controller.isKind(of: YourVC.self) {
                            navVC.popToViewController(controller, animated: true)
                            return
                        }
                    }
                }
extension UINavigationController {
    func popBack(to vc: AnyClass, animated: Bool = true) {
        guard let elementFound = (viewControllers.filter { $0.isKind(of: vc) }).first else {
            fatalError("cannot pop back to \(vc) as it is not in the view hierarchy")
        }
        self.popToViewController(elementFound, animated: animated)
    }
}

没有力的简单和最佳解决方案

if let vc = navigationController.viewControllers.filter({$0 is YourViewController}).first as? YourViewController {
            self.navigationController.popToViewController(vc, animated: true)
            }
本文地址:http://ios.askforanswer.com/ruhezaiswiftzhongdanchutedingdeview-controller.html
文章标签: ,   ,  
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!

文件下载

老薛主机终身7折优惠码boke112

上一篇:
下一篇:

评论已关闭!