DialogUtil.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // DialogUtil.m
  3. // wq
  4. //
  5. // Created by berwin on 13-6-28.
  6. // Copyright (c) 2013年 Weqia. All rights reserved.
  7. //
  8. #import "DialogUtil.h"
  9. @implementation DialogUtil
  10. + (DialogUtil *) sharedInstance
  11. {
  12. static DialogUtil *sharedInstance = nil ;
  13. static dispatch_once_t onceToken; // 锁
  14. dispatch_once (&onceToken, ^ { // 最多调用一次
  15. sharedInstance = [[self alloc] init];
  16. });
  17. return sharedInstance;
  18. }
  19. // 当第一次使用这个单例时,会调用这个init方法。
  20. - (id)init
  21. {
  22. self = [super init];
  23. if (self) {
  24. // 通常在这里做一些相关的初始化任务
  25. }
  26. return self;
  27. }
  28. + (void)showDlgAlert:(NSString *) label {
  29. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:Localized(@"JX_Tip") message:label delegate:self cancelButtonTitle:NSLocalizedStringFromTable(@"str_common_sure", @"local", nil) otherButtonTitles:nil, nil];
  30. [alertView setBackgroundColor:[UIColor clearColor]];
  31. //必须在这里调用show方法,否则indicator不在UIAlerView里面
  32. [alertView show];
  33. }
  34. - (void)showDlgCommon:(UIView *) view {
  35. // The hud will dispable all input on the view (use the higest view possible in the view hierarchy)
  36. MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
  37. [view addSubview:hud];
  38. // Regiser for HUD callbacks so we can remove it from the window at the right time
  39. hud.delegate = self;
  40. // Show the HUD while the provided method executes in a new thread
  41. [hud showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
  42. }
  43. - (void)showDlg:(UIView *) view withLabel:(NSString *) label {
  44. MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
  45. [view addSubview:hud];
  46. hud.delegate = self;
  47. hud.labelText = label;
  48. [hud showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
  49. }
  50. - (void)showDlg:(UIView *) view withLabel:(NSString *)label withDetail:(NSString *)detail {
  51. MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
  52. [view addSubview:hud];
  53. hud.delegate = self;
  54. hud.labelText = label;
  55. hud.detailsLabelText = detail;
  56. hud.square = YES;
  57. [hud showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
  58. }
  59. - (void)showDlg:(UIView *) view withLabelDeterminate:(NSString *) label{
  60. MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
  61. [view addSubview:hud];
  62. // Set determinate mode
  63. hud.mode = MBProgressHUDModeDeterminate;
  64. hud.delegate = self;
  65. hud.labelText = label;
  66. // myProgressTask uses the HUD instance to update progress
  67. [hud showWhileExecuting:@selector(myProgressTask:) onTarget:self withObject:hud animated:YES];
  68. }
  69. - (void)showDlg:(UIView *)view withLabelAnnularDeterminate:(NSString *) label {
  70. MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
  71. [view addSubview:hud];
  72. // Set determinate mode
  73. hud.mode = MBProgressHUDModeAnnularDeterminate;
  74. hud.delegate = self;
  75. hud.labelText = label;
  76. // myProgressTask uses the HUD instance to update progress
  77. [hud showWhileExecuting:@selector(myProgressTask:) onTarget:self withObject:hud animated:YES];
  78. }
  79. - (void)showDlgWithLabelDeterminateHorizontalBar:(UIView *) view {
  80. MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
  81. [view addSubview:hud];
  82. // Set determinate bar mode
  83. hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
  84. hud.delegate = self;
  85. // myProgressTask uses the HUD instance to update progress
  86. [hud showWhileExecuting:@selector(myProgressTask:) onTarget:self withObject:hud animated:YES];
  87. }
  88. - (void)showDlg:(UIView *) view withImage:(NSString *) imgName withLabel:(NSString *) label {
  89. MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
  90. [view addSubview:hud];
  91. // The sample image is based on the work by http://www.pixelpressicons.com, http://creativecommons.org/licenses/by/2.5/ca/
  92. // Make the customViews 37 by 37 pixels for best results (those are the bounds of the build-in progress indicators)
  93. hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imgName]];
  94. //@"37x-Checkmark.png"
  95. // Set custom view mode
  96. hud.mode = MBProgressHUDModeCustomView;
  97. hud.delegate = self;
  98. hud.labelText = label;
  99. [hud show:YES];
  100. [hud hide:YES afterDelay:2];
  101. }
  102. - (void)showDldLabelMixed:(UIView *) view {
  103. MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
  104. [view addSubview:hud];
  105. hud.delegate = self;
  106. hud.labelText = @"Connecting";
  107. hud.minSize = CGSizeMake(135.f, 135.f);
  108. [hud showWhileExecuting:@selector(myMixedTask:) onTarget:self withObject:hud animated:YES];
  109. }
  110. - (void)showDlg:(UIView *) view usingBlocks:(NSString *)label {
  111. #if NS_BLOCKS_AVAILABLE
  112. MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
  113. [view addSubview:hud];
  114. hud.labelText = label;
  115. [hud showAnimated:YES whileExecutingBlock:^{
  116. [self myTask];
  117. } completionBlock:^{
  118. [hud removeFromSuperview];
  119. }];
  120. #endif
  121. }
  122. - (void)showDlg:(UIView *) view onWindow:(NSString *) label {
  123. // The hud will dispable all input on the window
  124. MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view.window];
  125. [view.window addSubview:hud];
  126. hud.delegate = self;
  127. hud.labelText = label;
  128. [hud showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
  129. }
  130. - (void)showDlg:(UIView *) view witgURL:(NSString *) url {
  131. //@"https://github.com/matej/MBProgressHUD/zipball/master"
  132. NSURL *URL = [NSURL URLWithString:url];
  133. NSURLRequest *request = [NSURLRequest requestWithURL:URL];
  134. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
  135. [connection start];
  136. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
  137. hud.delegate = self;
  138. }
  139. - (void)showDlgWithGradient:(UIView *) view {
  140. MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
  141. [view addSubview:hud];
  142. hud.dimBackground = YES;
  143. // Regiser for HUD callbacks so we can remove it from the window at the right time
  144. hud.delegate = self;
  145. // Show the HUD while the provided method executes in a new thread
  146. [hud showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
  147. }
  148. - (void)showDlg:(UIView *) view textOnly:(NSString *) label {
  149. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
  150. // Configure for text only and offset down
  151. hud.mode = MBProgressHUDModeText;
  152. hud.labelText = label;
  153. hud.margin = 10.f;
  154. hud.yOffset = 150.f;
  155. hud.removeFromSuperViewOnHide = YES;
  156. [hud hide:YES afterDelay:1];
  157. }
  158. - (void)showDlg:(UIView *) view withColor:(UIColor *) color {
  159. MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
  160. [view addSubview:hud];
  161. // Set the hud to display with a color
  162. //[UIColor colorWithRed:0.23 green:0.50 blue:0.82 alpha:0.90]
  163. hud.color = color;
  164. hud.delegate = self;
  165. [hud showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
  166. }
  167. #pragma mark -
  168. #pragma mark Execution code
  169. - (void)myTask {
  170. // Do something usefull in here instead of sleeping ...
  171. sleep(1);
  172. }
  173. - (void)myProgressTask:(MBProgressHUD *) hud {
  174. // This just increases the progress indicator in a loop
  175. float progress = 0.0f;
  176. while (progress < 1.0f) {
  177. progress += 0.01f;
  178. hud.progress = progress;
  179. usleep(50000);
  180. }
  181. }
  182. - (void)myMixedTask:(MBProgressHUD *) hud {
  183. // Indeterminate mode
  184. sleep(2);
  185. // Switch to determinate mode
  186. hud.mode = MBProgressHUDModeDeterminate;
  187. hud.labelText = @"Progress";
  188. float progress = 0.0f;
  189. while (progress < 1.0f)
  190. {
  191. progress += 0.01f;
  192. hud.progress = progress;
  193. usleep(50000);
  194. }
  195. // Back to indeterminate mode
  196. hud.mode = MBProgressHUDModeIndeterminate;
  197. hud.labelText = @"Cleaning up";
  198. sleep(2);
  199. // The sample image is based on the work by www.pixelpressicons.com, http://creativecommons.org/licenses/by/2.5/ca/
  200. // Make the customViews 37 by 37 pixels for best results (those are the bounds of the build-in progress indicators)
  201. hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark"]];
  202. hud.mode = MBProgressHUDModeCustomView;
  203. hud.labelText = @"Completed";
  204. sleep(2);
  205. }
  206. #pragma mark -
  207. #pragma mark MBProgressHUDDelegate methods
  208. - (void)hudWasHidden:(MBProgressHUD *)hud {
  209. // Remove HUD from screen when the HUD was hidded
  210. [hud removeFromSuperview];
  211. hud = nil;
  212. }
  213. @end