Skip to content

Commit

Permalink
Added allHUDsForView:, and refactored existing methods to use it.
Browse files Browse the repository at this point in the history
- some additional cleanup and refactoring of class methods
  • Loading branch information
matej committed Mar 20, 2012
1 parent ecc2d11 commit ee3799c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 38 deletions.
32 changes: 21 additions & 11 deletions MBProgressHUD.h
Expand Up @@ -138,16 +138,6 @@ typedef enum {
*/
+ (MBProgressHUD *)showHUDAddedTo:(UIView *)view animated:(BOOL)animated;

/**
* Finds a HUD subview and returns it. This is used internally by hideHUDForFiew:animated:, but can also be useful externally.
*
* @param view The view that is going to be searched for a HUD subview.
* @return A reference to the last HUD subview discovered.
*
* @see hideHUDForView:animated:
*/
+ (MBProgressHUD *)findHUDForView:(UIView *)view;

/**
* Finds a HUD subview and hides it. The counterpart to this method is showHUDAddedTo:animated:.
*
Expand All @@ -170,7 +160,27 @@ typedef enum {
*
* @see hideAllHUDForView:animated:
*/
+ (int)hideAllHUDForView:(UIView *)view animated:(BOOL)animated;
+ (NSUInteger)hideAllHUDsForView:(UIView *)view animated:(BOOL)animated;

/**
* Finds a HUD subview and returns it. This is used internally by hideHUDForFiew:animated:, but can also be useful externally.
*
* @param view The view that is going to be searched for a HUD subview.
* @return A reference to the last HUD subview discovered.
*
* @see hideHUDForView:animated:
*/
+ (MBProgressHUD *)HUDForView:(UIView *)view;

/**
* Finds all HUD subviews and returns them. This is used internally by hideAllHUDsForView:animated:, but can also be useful externally.
*
* @param view The view that is going to be searched for HUD subviews.
* @return All found HUD views (array of MBProgressHUD objects).
*
* @see hideAllHUDsForView:animated:
*/
+ (NSArray *)allHUDsForView:(UIView *)view;

/**
* A convenience constructor that initializes the HUD with the window's bounds. Calls the designated constructor with
Expand Down
60 changes: 33 additions & 27 deletions MBProgressHUD.m
Expand Up @@ -228,42 +228,48 @@ + (MBProgressHUD *)showHUDAddedTo:(UIView *)view animated:(BOOL)animated {
#endif
}

+ (MBProgressHUD *)findHUDForView:(UIView *)view {
MBProgressHUD *hud = nil;
for (UIView *v in [view subviews]) {
if ([v isKindOfClass:[MBProgressHUD class]]) {
hud = (MBProgressHUD *)v;
}
}
return hud;
}

+ (BOOL)hideHUDForView:(UIView *)view animated:(BOOL)animated {
MBProgressHUD *viewToRemove = [MBProgressHUD findHUDForView:view];
if (viewToRemove != nil) {
MBProgressHUD *HUD = (MBProgressHUD *)viewToRemove;
HUD.removeFromSuperViewOnHide = YES;
[HUD hide:animated];
MBProgressHUD *hud = [MBProgressHUD HUDForView:view];
if (hud != nil) {
hud.removeFromSuperViewOnHide = YES;
[hud hide:animated];
return YES;
} else {
return NO;
}
return NO;
}

+ (int)hideAllHUDForView:(UIView *)view animated:(BOOL)animated {
int HUDCounter = 0;
+ (NSUInteger)hideAllHUDsForView:(UIView *)view animated:(BOOL)animated {
NSArray *huds = [self allHUDsForView:view];
for (MBProgressHUD *hud in huds) {
hud.removeFromSuperViewOnHide = YES;
[hud hide:animated];
}
return [huds count];
}

for (UIView *view in [view subviews]) {
if ([view isKindOfClass:[MBProgressHUD class]]) {
HUDCounter++;
MBProgressHUD *hud = (MBProgressHUD *)view;
hud.removeFromSuperViewOnHide = YES;
[hud hide:animated];
+ (MBProgressHUD *)HUDForView:(UIView *)view {
MBProgressHUD *hud = nil;
NSArray *subviews = view.subviews;
Class hudClass = [MBProgressHUD class];
for (UIView *view in subviews) {
if ([view isKindOfClass:hudClass]) {
hud = (MBProgressHUD *)view;
}
}
return hud;
}

return HUDCounter;
};
+ (NSArray *)allHUDsForView:(UIView *)view {
NSMutableArray *huds = [NSMutableArray array];
NSArray *subviews = view.subviews;
Class hudClass = [MBProgressHUD class];
for (UIView *view in subviews) {
if ([view isKindOfClass:hudClass]) {
[huds addObject:view];
}
}
return [NSArray arrayWithArray:huds];
}

#pragma mark -
#pragma mark Lifecycle methods
Expand Down

0 comments on commit ee3799c

Please sign in to comment.