func getCurrentViewController(with rootViewController: UIViewController!)->UIViewController?{
if nil == rootViewController{
return nil
}
// UITabBarController就接着查找它当前显示的selectedViewController
if rootViewController.isKind(of: UITabBarController.self){
return self.getCurrentViewController(with: (rootViewController as! UITabBarController).selectedViewController)
// UINavigationController就接着查找它当前显示的visibleViewController
}else if rootViewController.isKind(of: UINavigationController.self){
return self.getCurrentViewController(with: (rootViewController as! UINavigationController).visibleViewController)
// 如果当前窗口有presentedViewController,就接着查找它的presentedViewController
}else if nil != rootViewController.presentedViewController{
return self.getCurrentViewController(with: rootViewController.presentedViewController)
}
// 否则就代表找到了当前显示的控制器
return rootViewController
}
//调用方式
let rootVC = UIApplication.shared.keyWindow?.rootViewController
let currentVC = getCurrentViewController(with: rootVC)
swift返回当前控制器
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- /* UIViewController is a generic controller base class th...
- 工作了好久一直很忙,好不容易有些时间,今天有人问我如何开始写一个项目,不禁回忆起自己写第一个项目的时候,到现在,突...