MBProgressHud第二次看后没有隐藏

时间:2012-08-07 14:32:55

标签: iphone mbprogresshud

切换标签时,我想要显示MBProgressHUD。这是在我的应用代表中。我这里有这个代码只显示前三个选项卡上的HUD

 -(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];            
    }
  }
}

我第一次查看我的页面时它可以工作,但第二次回到它不会隐藏它。我的[appDel.HUD hide:YES afterDelay:1.0];中有viewDidAppear

每次访问页面时如何隐藏HUD?

2 个答案:

答案 0 :(得分:0)

我认为你可能会将MBProgressHUD添加到UINavigationController中的不同视图中。然后你弹出到root,它试图删除一个不存在的MBProgressHUD。试试这个(未经测试的代码)......

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];
}   

答案 1 :(得分:0)

你必须检查MBProgressHUD视图是否已被激活,如果是这样,或者只是这样做:

if (!HUD) {
    [self showLoadingHUD];
}