美文网首页
swift vc多层push后回到指定页面

swift vc多层push后回到指定页面

作者: 笙绳省盛 | 来源:发表于2018-11-05 16:30 被阅读5次
    //获取当前的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)
    

    相关文章

      网友评论

          本文标题:swift vc多层push后回到指定页面

          本文链接:https://www.haomeiwen.com/subject/wuvsxqtx.html