4

I am using MBProgressHUD to show an activity indicator while downloading web service data. The app will be often be used in rural areas with poor connectivity so I want to be able to set a timeout for the HUD to say, 10 seconds (as just an arbitrary figure). I'm not sure how to go about this. Can anyone offer suggestions? THanks!

MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];

NSString *info = [NSString stringWithFormat:@"Loading %@ gauges", self.stateIdentifier];
[hud setLabelText:info];
[hud setDetailsLabelText:@"Please wait..."];
[hud setDimBackground:YES];
[hud setOpacity:0.5f];
[hud show:YES];

[self.view addSubview:hud];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    stateGauges = [[GaugeList alloc] initWithStateIdentifier:stateIdentifier andType:nil];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
        [hud removeFromSuperview];
    });
});
bhawesh
  • 1,310
  • 2
  • 19
  • 57
Pheepster
  • 6,045
  • 6
  • 41
  • 75

2 Answers2

11

You can use the:

- (void)hide:(BOOL)animated afterDelay:(NSTimeInterval)delay;

method of MBProgressHUD.

Create MBProgressHUD like:

MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:hud];
NSString *info = [NSString stringWithFormat:@"Loading %@ gauges", self.stateIdentifier];
[hud setLabelText:info];
[hud setDetailsLabelText:@"Please wait..."];
[hud setDimBackground:YES];
[hud setOpacity:0.5f];
[hud show:YES];
[hud hide:YES afterDelay:10.0];
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • 1
    Thanks for your answer, but after trying this, it has no effect on the HUD. I tried it by setting the timeout to one second. – Pheepster Dec 30 '13 at 19:40
  • You also need to add the _hud_ to your view, when initialising _MBProgressHUD_ this this. `[self.view addSubview:hud]`. Just calling `show:` is not sufficient. – TheCommonEngineer Jan 07 '16 at 12:42
  • @abhilash1912: You can pass a view to MBProgressHUD while initialising it (As shown in above code). In previous versions of MBProgressHUD that was enough, but in latest versions you need to add it to the view using addSubView: – Midhun MP Jan 07 '16 at 18:30
  • Yes, I was referring to the latest version only. It would helpful for others if you update your answers to mention that. Thanks. – TheCommonEngineer Jan 07 '16 at 19:02
  • @abhilash1912: Thanks for the comment, updated the code :) – Midhun MP Jan 07 '16 at 19:11
1

For Swift, following is the solution:

let progressHUD = MBProgressHUD.showHUDAddedTo(self.view, animated: true)

progressHUD.hideAnimated(true, afterDelay: 5)