Skip to content

Commit 62771b4

Browse files
committed
Added a new method (hid:afterDelay:).
1 parent f6b9d49 commit 62771b4

File tree

3 files changed

+37
-58
lines changed

3 files changed

+37
-58
lines changed

Demo/Classes/HudDemoViewController.m

Lines changed: 18 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -46,111 +46,80 @@ - (void)dealloc {
4646
- (IBAction)showSimple:(id)sender {
4747
// The hud will dispable all input on the view (use the higest view possible in the view hierarchy)
4848
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
49-
50-
//HUD.graceTime = 0.5;
51-
//HUD.minShowTime = 5.0;
52-
53-
// Add HUD to screen
5449
[self.navigationController.view addSubview:HUD];
5550

56-
// Regisete for HUD callbacks so we can remove it from the window at the right time
51+
// Regiser for HUD callbacks so we can remove it from the window at the right time
5752
HUD.delegate = self;
5853

5954
// Show the HUD while the provided method executes in a new thread
6055
[HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
6156
}
6257

6358
- (IBAction)showWithLabel:(id)sender {
64-
// The hud will dispable all input on the view (use the higest view possible in the view hierarchy)
59+
6560
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
61+
[self.navigationController.view addSubview:HUD];
6662

67-
// Add HUD to screen
68-
[self.navigationController.view addSubview:HUD];
69-
70-
// Regisete for HUD callbacks so we can remove it from the window at the right time
7163
HUD.delegate = self;
72-
7364
HUD.labelText = @"Loading";
7465

75-
// Show the HUD while the provided method executes in a new thread
7666
[HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
7767
}
7868

7969
- (IBAction)showWithDetailsLabel:(id)sender {
80-
// The hud will dispable all input on the view (use the higest view possible in the view hierarchy)
81-
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
8270

83-
// Add HUD to screen
71+
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
8472
[self.navigationController.view addSubview:HUD];
8573

86-
// Regisete for HUD callbacks so we can remove it from the window at the right time
8774
HUD.delegate = self;
88-
8975
HUD.labelText = @"Loading";
9076
HUD.detailsLabelText = @"updating data";
9177

92-
// Show the HUD while the provided method executes in a new thread
9378
[HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
9479
}
9580

9681
- (IBAction)showWithLabelDeterminate:(id)sender {
97-
// The hud will dispable all input on the view (use the higest view possible in the view hierarchy)
82+
9883
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
84+
[self.navigationController.view addSubview:HUD];
9985

10086
// Set determinate mode
10187
HUD.mode = MBProgressHUDModeDeterminate;
102-
103-
// Add HUD to screen
104-
[self.navigationController.view addSubview:HUD];
105-
106-
// Regisete for HUD callbacks so we can remove it from the window at the right time
107-
HUD.delegate = self;
108-
88+
89+
HUD.delegate = self;
10990
HUD.labelText = @"Loading";
11091

111-
// Show the HUD while the provided method executes in a new thread
92+
// myProgressTask uses the HUD instance to update progress
11293
[HUD showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES];
11394
}
11495

11596
- (IBAction)showWithCustomView:(id)sender {
116-
// The hud will dispable all input on the view (use the higest view possible in the view hierarchy)
97+
11798
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
99+
[self.navigationController.view addSubview:HUD];
118100

119-
// The sample image is based on the work by www.pixelpressicons.com, http://creativecommons.org/licenses/by/2.5/ca/
101+
// The sample image is based on the work by http://www.pixelpressicons.com, http://creativecommons.org/licenses/by/2.5/ca/
120102
// Make the customViews 37 by 37 pixels for best results (those are the bounds of the build-in progress indicators)
121103
HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]] autorelease];
122104

123105
// Set custom view mode
124106
HUD.mode = MBProgressHUDModeCustomView;
125107

126-
// Add HUD to screen
127-
[self.navigationController.view addSubview:HUD];
128-
129-
// Regisete for HUD callbacks so we can remove it from the window at the right time
130108
HUD.delegate = self;
131-
132109
HUD.labelText = @"Completed";
133110

134-
// This would only show the completed text with no visible custom view
135-
// HUD.customView = [[UIView alloc] initWithFrame:CGRectZero];
136-
137-
// Show the HUD while the provided method executes in a new thread
138-
[HUD showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES];
111+
[HUD show:YES];
112+
[HUD hide:YES afterDelay:3];
139113
}
140114

141115
- (IBAction)showWithLabelMixed:(id)sender {
142-
// The hud will dispable all input on the view (use the higest view possible in the view hierarchy)
116+
143117
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
118+
[self.navigationController.view addSubview:HUD];
144119

145-
// Add HUD to screen
146-
[self.navigationController.view addSubview:HUD];
147-
148-
// Regisete for HUD callbacks so we can remove it from the window at the right time
149120
HUD.delegate = self;
150-
151121
HUD.labelText = @"Connecting";
152122

153-
// Show the HUD while the provided method executes in a new thread
154123
[HUD showWhileExecuting:@selector(myMixedTask) onTarget:self withObject:nil animated:YES];
155124
}
156125

@@ -161,12 +130,8 @@ - (IBAction)showUsingBlocks:(id)sender {
161130
hud.labelText = @"Loading";
162131

163132
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
164-
165133
// Do a taks in the background
166134
[self myTask];
167-
hud.labelText = @"Some more";
168-
[self myTask];
169-
170135
// Hide the HUD in the main tread
171136
dispatch_async(dispatch_get_main_queue(), ^{
172137
[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
@@ -176,18 +141,13 @@ - (IBAction)showUsingBlocks:(id)sender {
176141
}
177142

178143
- (IBAction)showOnWindow:(id)sender {
179-
// The hud will dispable all input on the view
144+
// The hud will dispable all input on the window
180145
HUD = [[MBProgressHUD alloc] initWithView:self.view.window];
181-
182-
// Add HUD to screen
183146
[self.view.window addSubview:HUD];
184147

185-
// Regisete for HUD callbacks so we can remove it from the window at the right time
186148
HUD.delegate = self;
187-
188149
HUD.labelText = @"Loading";
189150

190-
// Show the HUD while the provided method executes in a new thread
191151
[HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
192152
}
193153

@@ -241,6 +201,7 @@ - (void)hudWasHidden {
241201
// Remove HUD from screen when the HUD was hidded
242202
[HUD removeFromSuperview];
243203
[HUD release];
204+
HUD = nil;
244205
}
245206

246207
@end

MBProgressHUD.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,14 +285,24 @@ typedef enum {
285285
- (void)show:(BOOL)animated;
286286

287287
/**
288-
* Hide the HUD, this still calls the hudWasHidden delegate. This is the counterpart of the hide: method. Use it to
288+
* Hide the HUD. This still calls the hudWasHidden delegate. This is the counterpart of the hide: method. Use it to
289289
* hide the HUD when your task completes.
290290
*
291291
* @param animated If set to YES the HUD will disappear using the current animationType. If set to NO the HUD will not use
292292
* animations while disappearing.
293293
*/
294294
- (void)hide:(BOOL)animated;
295295

296+
/**
297+
* Hide the HUD after a delay. This still calls the hudWasHidden delegate. This is the counterpart of the hide: method. Use it to
298+
* hide the HUD when your task completes.
299+
*
300+
* @param animated If set to YES the HUD will disappear using the current animationType. If set to NO the HUD will not use
301+
* animations while disappearing.
302+
* @param delay Delay in secons until the HUD is hidden.
303+
*/
304+
- (void)hide:(BOOL)animated afterDelay:(NSTimeInterval)delay;
305+
296306
/**
297307
* Shows the HUD while a background task is executing in a new thread, then hides the HUD.
298308
*

MBProgressHUD.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,14 @@ - (void)hide:(BOOL)animated {
437437
[self hideUsingAnimation:useAnimation];
438438
}
439439

440+
- (void)hide:(BOOL)animated afterDelay:(NSTimeInterval)delay {
441+
[self performSelector:@selector(hideDelayed:) withObject:[NSNumber numberWithBool:delay] afterDelay:delay];
442+
}
443+
444+
- (void)hideDelayed:(NSNumber *)animated {
445+
[self hide:[animated boolValue]];
446+
}
447+
440448
- (void)handleGraceTimer:(NSTimer *)theTimer {
441449
// Show the HUD only if the task is still running
442450
if (taskInProgress) {

0 commit comments

Comments
 (0)