iOS开发获取当前控制器的方法
func getCurrentController() -> UIViewController? {
guard let window = UIApplication.sharedApplication().windows.first else {
return nil
}
var tempView = window.subviews.last
for subview in window.subviews.reverse() {
if subview.classForCoder.description() == "UILayoutContainerView" {
tempView = subview
break
}
}
var nextResponder = tempView?.nextResponder()
var next: Bool {
return !(nextResponder is UIViewController) || nextResponder is UINavigationController || nextResponder is UITabBarController
}
while next{
tempView = tempView?.subviews.first
if tempView == nil {
return nil
}
nextResponder = tempView!.nextResponder()
}
return nextResponder as? UIViewController
}
}