2

I am using MBProgressHUD for showing loading while performing local long method to put user good experience. All working fine but now i have to implement like that if the method executes within a second than the loading should not be appear. I have gone through the MBProgressHUD samples and for this functionality i found the setGraceTime and setMinShowTime show time but its all not working properly. because when i put set grace time the loading icon is not appear even the method execution time is more than 1 second.Here is my code

    if (self.HUD == nil)
    {
        self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
        [self.view addSubview:self.HUD];
    }
    self.HUD.labelText = @"Please wait.....";
    //        [self.HUD setMinShowTime:2.0f];

    [self.HUD setGraceTime:1.0f];
    [self.HUD setTaskInProgress:YES];
    [self.HUD show:YES];
    //        [self.HUD hide:YES afterDelay:3];

    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantPast]];

    if (mycondition == nil)
    {
            [self myTask:[NSURL fileURLWithPath:strPath]];
            //[self performSelector:@selector(mytask:) withObject:[NSURL fileURLWithPath:strPath]];
    }
    else
    {
      //else
    }

    self.tempView.hidden = YES;
    //        self.HUD.taskInProgress = NO;
    [self.HUD hide:YES];
    self.HUD = nil;

I have putted [[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantPast]]; for this problem's solution please guide me what wrong in this code..?!

Community
  • 1
  • 1
Jaimin Modi
  • 139
  • 1
  • 2
  • 12

1 Answers1

1

You need to remove the MBProgressHUD object from its superview in the method execution. Like this:

// if the method runs inside one second, the indicator will not be shown

- (void)viewDidLoad
{
  [super viewDidLoad];
  if (HUD == nil)
  {
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
  }
  HUD.labelText = @"Please wait.....";
  [HUD setGraceTime:1.0f];
  [HUD setTaskInProgress:YES];
  [HUD show:YES];
  [self performSelector:@selector(runThisMethod) withObject:nil afterDelay:0.9f];
}

- (void)runThisMethod
{
    [HUD removeFromSuperview];
}

// if the method runs after one second, the indicator will be shown for some time until the method runs

- (void)viewDidLoad
{
  [super viewDidLoad];
  if (HUD == nil)
  {
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
  }
  HUD.labelText = @"Please wait.....";
  [HUD setGraceTime:1.0f];
  [HUD setTaskInProgress:YES];
  [HUD show:YES];
  [self performSelector:@selector(runThisMethod) withObject:nil afterDelay:1.9f];
}

- (void)runThisMethod
{
    [HUD removeFromSuperview];
}
Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33
  • this is not working for me but my code is working when i run my application with putting break point in `show` method of `MBProgressHUD` don't know why this happens.. – Jaimin Modi Aug 02 '13 at 06:28