IOS获取当前页面的UIViewController
在开发中我们经常需要在当前页面提示Tip信息或者Alert,那么比较通用的方法是通过通用的方法拿到当前页面的VC。在MBProgressHUD中我们也需要一个工具类来进行相应的提示。
/**
获得当前页面
@return 获得当前页面
*/
+(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 {
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];
result = nav.childViewControllers.lastObject;
} else if ([nextResponder isKindOfClass:[UINavigationController class]]){
UIViewController * nav = (UIViewController *)nextResponder;
result = nav.childViewControllers.lastObject;
} else {
result = nextResponder;
}
return result;
}
工具类简单封装
#import "HUDUtil.h"
#import "MBProgressHUD.h"
#import "UtilMacros.h"
#import "AppDelegate.h"
#import "StringMacros.h"
@implementation HUDUtil
/**
显示alert
*/
+(void)showAlertWithTitle:(NSString *)title andContent:(NSString *)content {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:content preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
[alertController addAction:okAction];
[[HUDUtil getCurrentVC] presentViewController:alertController animated:YES completion:nil];
}
/**
显示loading
*/
+(void)showLoading {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[HUDUtil getCurrentVC].view animated:YES];
hud.label.text = Tip_Loading;
hud.label.font = [UIFont systemFontOfSize:14.0f];
}
/**
隐藏loading
*/
+(void)hiddenLoading {
[MBProgressHUD hideHUDForView:[HUDUtil getCurrentVC].view animated:YES];
}
/**
显示loading
@param aView 目标显示层
*/
+(void)showLoadingWithView:(UIView *)aView {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:aView animated:YES];
hud.label.text = Tip_Loading;
hud.label.font = [UIFont systemFontOfSize:14.0f];
}
/**
隐藏loading
@param aView 目标显示层
*/
+(void)hiddenLoadingWithView:(UIView *)aView {
[MBProgressHUD hideHUDForView:aView animated:YES];
}
/**
显示提示框
@param message 提示信息
*/
+ (void)showTipWithHud:(NSString*)message{
UIView *view = [UIApplication sharedApplication].keyWindow;
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
[view bringSubviewToFront:hud];
[hud setOffset:CGPointMake(hud.offset.x, SCREEN_HEIGHT/4)];
hud.mode = MBProgressHUDModeText;
hud.detailsLabel.text = message;
hud.margin = 10.f;
hud.removeFromSuperViewOnHide = YES;
[hud hideAnimated:YES afterDelay:1];
}
/**
获得当前页面
@return 获得当前页面
*/
+(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 {
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];
result = nav.childViewControllers.lastObject;
} else if ([nextResponder isKindOfClass:[UINavigationController class]]){
UIViewController * nav = (UIViewController *)nextResponder;
result = nav.childViewControllers.lastObject;
} else {
result = nextResponder;
}
return result;
}
@end
网友评论