//获取当前的nvc
func getCurrentNavigationController()->UINavigationController?{
var parent: UIViewController?
if let window = UIApplication.shared.delegate?.window,let rootVC = window?.rootViewController {
parent = rootVC
while (parent?.presentedViewController != nil) {
parent = parent?.presentedViewController!
}
if let tabbar = parent as? UITabBarController ,let nav = tabbar.selectedViewController as? UINavigationController {
return nav
}else if let nav = parent as? UINavigationController {
return nav
}
}
return nil
}
//pop回指定类名的控制器
func backToController(ctrlClassName : String,animated : Bool){
let nvc = getCurrentNavigationController()
if nvc != nil{
let controllers = nvc?.viewControllers as! NSArray
let result = controllers.filtered(using: NSPredicate(block: { (evaluatedObject, bindings) -> Bool in
return (evaluatedObject as AnyObject).isKind(of: NSClassFromString(ctrlClassName)!)
}))
if result.count > 0 {
nvc?.popToViewController(result[0] as! UIViewController, animated: animated)
}
}
}
调用
backToController(ctrlClassName: "AViewController", animated: true)
网友评论