获取当前控制器(如果是在tabbarController中也能正确的获得当前控制器,而不是tabbarController)



+ (UIViewController *)getCurrentVC { UIWindow * window = [[UIApplication sharedApplication] keyWindow]; if (window.windowLevel != UIWindowLevelNormal){ NSArray *windows = [[UIApplication sharedApplication] windows]; for(UIWindow * tmpWin in windows){ if (tmpWin.windowLevel == UIWindowLevelNormal){ window = tmpWin; break; } } } UIViewController *result = window.rootViewController; while (result.presentedViewController) { result = result.presentedViewController; } if ([result isKindOfClass:[UITabBarController class]]) { result = [(UITabBarController *)result selectedViewController]; } if ([result isKindOfClass:[UINavigationController class]]) { result = [(UINavigationController *)result topViewController]; } return result; }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM