美文网首页
swift 获取app当前视图控制器,回退到根控制器

swift 获取app当前视图控制器,回退到根控制器

作者: 陈江红 | 来源:发表于2018-08-14 19:41 被阅读244次

获取当前的控制器,代码如下

func currentViewController() -> UIViewController{

    var currentVC: UIViewController? = self.window?.rootViewController

    while (current?.presentedViewController != nil) {

        currentVC = currentVC.presentedViewController

    }

    if (currentVC?.isKind(of: UITaberViewController.self))! {

        currentVC = (currentVC as! UITaberViewController).selectedViewController

    }

    if (currentVC?.isKind(of: UINavigationController.self))! {

        currentVC = (currentVC as! UINavigationController).visibleViewController

    }

    return currentVC!

}

回退到根控制器

func backToRoot() {

    let currentVC = self.currentVIewController()

    var rootVC = (current as UIViewController).presentingViewController

if rootVC != nil {

    while( rootVC.presentingVIewCOntroller != nil) {

        rootVC = rootVC.presentingViewController

    }

    if ((rootVC?.responds(to: #selector(UIViewController.dismiss(animated:completion:))) == true){

        (root as! UINavigationController).popToRootViewController(animated:false)

    }

 }else{

currentVC.navigationController?.popToRootViewController(animated:false)

 }

}

相关文章

网友评论

      本文标题:swift 获取app当前视图控制器,回退到根控制器

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