首页 > 代码库 > 第十六篇、MBprogressHUD的使用和获取当前的控制器
第十六篇、MBprogressHUD的使用和获取当前的控制器
#import "AppDelegate+HUD.h"#import "MBProgressHUD.h"@implementation AppDelegate (HUD)// 获取当前处于activity状态的view controller- (UIViewController *)activityViewController{ UIViewController* activityViewController = nil; UIWindow *window = [[UIApplication sharedApplication] keyWindow]; // 如果是模态控制器用这个方法获取 UIViewController *appRootVC = window.rootViewController; UIViewController *topVC = appRootVC; if (topVC.presentedViewController) { topVC = topVC.presentedViewController; return topVC; } if(window.windowLevel != UIWindowLevelNormal) { NSArray *windows = [[UIApplication sharedApplication] windows]; for(UIWindow *tmpWin in windows) { if(tmpWin.windowLevel == UIWindowLevelNormal) { window = tmpWin; break; } } } NSArray *viewsArray = [window subviews]; if([viewsArray count] > 0) { UIView *frontView = [viewsArray objectAtIndex:0]; id nextResponder = [frontView nextResponder]; if([nextResponder isKindOfClass:[UIViewController class]]) { activityViewController = nextResponder; } else { activityViewController = window.rootViewController; } } return activityViewController;}/** * 因为网络请求是在异步线程中执行,只有主线程才可以更新UI */// 显示加载- (void) showHUD:(NSString *)msg{ dispatch_async(dispatch_get_main_queue(), ^{ if ([self.window viewWithTag:88888]) { [[self.window viewWithTag:88888] removeFromSuperview]; } MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.window]; HUD.tag = 88888; [self.window addSubview:HUD]; HUD.label.text = msg; HUD.contentColor = [UIColor blackColor]; HUD.label.textColor = [UIColor colorWithRed:254.0/255 green:71.0/255 blue:67.0/255 alpha:1]; [HUD setUserInteractionEnabled:NO]; // 设置成非模态显示 [HUD showAnimated:YES]; }); }// 移除- (void) removeHUD{ dispatch_async(dispatch_get_main_queue(), ^{ [[self.window viewWithTag:88888] hideAnimated:YES]; [[self.window viewWithTag:88888] removeFromSuperViewOnHide]; }); }// 显示信息- (void) showMessage:(NSString *) msg{ dispatch_async(dispatch_get_main_queue(), ^{ [[self.window viewWithTag:88888] hideAnimated:YES]; [[self.window viewWithTag:88888] removeFromSuperViewOnHide]; MBProgressHUD *HUDText = [MBProgressHUD showHUDAddedTo:self.window animated:YES]; // 设置只是显示文本 HUDText.tag = 88888; HUDText.mode = MBProgressHUDModeText; HUDText.label.text = msg; HUDText.margin = 10.0f; // CGPoint currentOffset = HUDText.offset; // currentOffset.y = -[UIScreen mainScreen].bounds.size.height / 2 + 80; // 设置Y的起始位置 // HUDText.offset = currentOffset; HUDText.removeFromSuperViewOnHide = YES; [HUDText setUserInteractionEnabled:NO]; [HUDText hideAnimated:YES afterDelay:2.8]; });}
第十六篇、MBprogressHUD的使用和获取当前的控制器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。