0

I have a MBProgressHUD that I want to display when switching tabs. This is in my app delegate. I have this code here to display the HUD on only the first three tabs

 -(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{

  if ([viewController isKindOfClass:[UINavigationController class]]) {  
    if (tabBarController.selectedIndex >= 3) {

        UINavigationController *nav = (UINavigationController *) viewController;
        [nav popToRootViewControllerAnimated:NO];            
    }

    else {
        UINavigationController *nav = (UINavigationController *) viewController;

        HUD = [[MBProgressHUD alloc] initWithView:nav.view];
        [nav.view addSubview:HUD]; 
        HUD.labelText = @"Loading";
        [HUD show:YES];

        [nav popToRootViewControllerAnimated:NO];            
    }
  }
}

The first time I view my page it works but going back to it a second time it doesn't hide. I have my [appDel.HUD hide:YES afterDelay:1.0]; in my viewDidAppear.

How can I get the HUD to hide every time I visit the page?

BigT
  • 1,413
  • 5
  • 24
  • 52

2 Answers2

0

I think you may be adding the MBProgressHUD to a different view within the UINavigationController. Then you're popping to the root, and it's trying to remove an MBProgressHUD that doesn't exist. Try this (untested code)...

UINavigationController *nav = (UINavigationController *) viewController;
[nav popToRootViewControllerAnimated:NO];

if(![nav.visibleViewController.view.subViews containsObject:MBProgressHUD])
{
    [MBProgressHUD showHUDAddedTo:nav.visibleViewController.view animated:YES];
    MBProgressHUD.labelText = @"Loading";  
}
else
{
     [MBProgressHUD setHidden:NO];
}   
Ben Boral
  • 355
  • 3
  • 11
  • this is in the else part of my `didSelectViewController` correct? – BigT Aug 07 '12 at 15:50
  • Yes. Within the else brackets. Sorry for not being clear about that. – Ben Boral Aug 07 '12 at 15:56
  • Well I figured this is where it goes but it still does not work. The first time works but not the second. – BigT Aug 07 '12 at 16:08
  • Sorry, the code I was writing was not tested. I meant to write nav.visibleViewController.view.subviews (subviews is a property of UIView) – Ben Boral Aug 08 '12 at 02:17
  • The HUD will never get displayed this way. the line `if(HUD!=nil)` will never go into because the HUD was never created. – BigT Aug 08 '12 at 13:38
0

you have to check if the MBProgressHUD view was already activated if so hid it, or simply do so:

if (!HUD) {
    [self showLoadingHUD];
}
polarware
  • 2,499
  • 1
  • 24
  • 18