在无论任何环境下,没有任何参数,我都要获得当前屏幕所显示的视图控制器
oc
+(UIViewController *)getCurrentVC
{
UIViewController *result = nil;
UIWindow * window = [[UIApplication sharedApplication] keyWindow];
//app默认windowLevel是UIWindowLevelNormal,如果不是,找到UIWindowLevelNormal的
if (window.windowLevel != UIWindowLevelNormal)
{
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow * tmpWin in windows)
{
if (tmpWin.windowLevel == UIWindowLevelNormal)
{
window = tmpWin;
break;
}
}
}
id nextResponder = nil;
UIViewController *appRootVC=window.rootViewController;
// 如果是present上来的appRootVC.presentedViewController 不为nil
if (appRootVC.presentedViewController) {
nextResponder = appRootVC.presentedViewController;
}else{
NSLog(@"===%@",[window subviews]);
UIView *frontView = [[window subviews] objectAtIndex:0];
nextResponder = [frontView nextResponder];
}
if ([nextResponder isKindOfClass:[UITabBarController class]]){
UITabBarController * tabbar = (UITabBarController *)nextResponder;
UINavigationController * nav = (UINavigationController *)tabbar.viewControllers[tabbar.selectedIndex];
// UINavigationController * nav = tabbar.selectedViewController ; 上下两种写法都行
result=nav.childViewControllers.lastObject;
}else if ([nextResponder isKindOfClass:[UINavigationController class]]){
UIViewController * nav = (UIViewController *)nextResponder;
result = nav.childViewControllers.lastObject;
}else{
result = nextResponder;
}
return result;
}
swift
// 找到当前显示的window
private func getCurrentWindow() -> UIWindow? {
// 找到当前显示的UIWindow
var window: UIWindow? = UIApplication.shared.keyWindow
/**
window有一个属性:windowLevel
当 windowLevel == UIWindowLevelNormal 的时候,表示这个window是当前屏幕正在显示的window
*/
if window?.windowLevel != UIWindow.Level.normal {
for tempWindow in UIApplication.shared.windows {
if tempWindow.windowLevel == UIWindow.Level.normal {
window = tempWindow
break
}
}
}
return window
}
/* 递归找最上面的viewController */
func getCurrentVC() -> UIViewController? {
return self.topViewControllerWithRootViewController(viewController: self.getCurrentWindow()?.rootViewController)
}
private func topViewControllerWithRootViewController(viewController :UIViewController?) -> UIViewController? {
if viewController == nil {
return nil
}
if viewController?.presentedViewController != nil {
return self.topViewControllerWithRootViewController(viewController: viewController?.presentedViewController!)
}
else if viewController?.isKind(of: UITabBarController.self) == true {
return self.topViewControllerWithRootViewController(viewController: (viewController as! UITabBarController).selectedViewController)
}
else if viewController?.isKind(of: UINavigationController.self) == true {
return self.topViewControllerWithRootViewController(viewController: (viewController as! UINavigationController).visibleViewController)
}
else {
return viewController
}
}
通过view获取vc
/** 通过当前view, 获取栈底controller */
func pgg_ViewController() -> (UIViewController) {
//1.通过响应者链关系,取得此视图的下一个响应者
var next:UIResponder?
next = self.next!
repeat {
//2.判断响应者对象是否是视图控制器类型
if ((next as?UIViewController) != nil) {
return (next as! UIViewController)
}else {
next = next?.next
}
} while next != nil
return UIViewController()
}
获取当前视图所在导航控制器
///获取当前视图所在导航控制器
func currentNavViewController() -> UINavigationController? {
var n = next
while n != nil {
if n is UINavigationController {
return n as? UINavigationController
}
n = n?.next
}
return nil
}
+(UIViewController *)getCurrentVC
{
UIViewController* vc = [UIApplication sharedApplication].keyWindow.rootViewController;
while (1) {
if ([vc isKindOfClass:[UITabBarController class]]) {
vc = ((UITabBarController*)vc).selectedViewController;
}
if ([vc isKindOfClass:[UINavigationController class]]) {
vc = ((UINavigationController*)vc).visibleViewController;
}
if (vc.presentedViewController) {
vc = vc.presentedViewController;
}else{
break;
}
}
return vc;
}