Question

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];
    });
});
Was it helpful?

Solution

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

OTHER TIPS

For Swift, following is the solution:

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

progressHUD.hideAnimated(true, afterDelay: 5)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top