func getCurrentVC()->UIViewController{
var window = UIApplication.sharedApplication().keyWindow
if window?.windowLevel != UIWindowLevelNormal{
let windows = UIApplication.sharedApplication().windows
for tempwin in windows{
if tempwin.windowLevel == UIWindowLevelNormal{
window = tempwin
break
}
}
}
let frontView = (window?.subviews)![0]
let nextResponder = frontView.nextResponder()
Getdevice.println("getCurrentVC XX \(frontView.classForCoder)")// iOS8 9 window ios7 UIView
Getdevice.println("getCurrentVC XX \((window?.subviews)!.count)")
Getdevice.println("getCurrentVC XX \(nextResponder?.classForCoder)")
if nextResponder?.isKindOfClass(UIViewController.classForCoder()) == true{
return nextResponder as! UIViewController
}else if nextResponder?.isKindOfClass(UINavigationController.classForCoder()) == true{
return (nextResponder as! UINavigationController).visibleViewController!
}
else {
if (window?.rootViewController) is UINavigationController{
return ((window?.rootViewController) as! UINavigationController).visibleViewController!//只有这个是显示的controller 是可以的必须有nav才行
}else if (window?.rootViewController) is BoosjTabBarViewController{
return ((window?.rootViewController) as! UITabBarController).selectedViewController! //不行只是最三个开始的页面
}
return (window?.rootViewController)!
}
}
里面有部分测试的代码,发现ios9 和ios8 上会找的的是window ios7 上是view 能找到ViewController, 所以我在后面直接返回nav 的visibleViewController 其他的是测试返回可以删除