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

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

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)

 }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 今夜还昨年,随意眺望间。 只是近分别,光影倏离乱。 逝者不反兮,前情忽超远。 忆惜起往日,你我初相见。 何日将惨离...
    道国行人阅读 315评论 0 2
  • 动感超人丶阅读 240评论 0 0
  • 从文章的概述,我们能够了解到表情包的发展史,已经有几十年了,任何一个物质的存在都有其合理性,尤其是经过时间筛选存在...
    丿子木丨阅读 428评论 0 0
  • 五月份读书营的主题活动——精读营,十天读一本书,这次我读的书是《别再为小事抓狂》。按照精读营的要求,每天在23:0...
    范范_嘟嘟阅读 440评论 1 4
  • 逝者如斯夫,不舍昼夜。 ——孔子 时光如白驹过隙,一天天、一月月,眼看着在我们的指...
    生如夏花2018阅读 780评论 6 14