MBProgressHUD宽限期和最短时间不能正常工作。如何实施。?

时间:2013-08-02 04:17:48

标签: ios mbprogresshud

我正在使用MBProgressHUD来显示加载,同时执行本地长方法以获得良好的用户体验。所有工作正常,但现在我必须实现,如果方法在一秒钟内执行,不应该出现加载。我已经浏览了MBProgressHUD示例,为了实现此功能,我发现setGraceTimesetMinShowTime显示时间但是它们都无法正常工作。因为当我设置宽限时间时,即使方法执行时间超过1秒,也不会出现加载图标。这是我的代码

    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;

我已为this problem's solution 推出[[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantPast]]; 请指导我这段代码有什么问题..?!

1 个答案:

答案 0 :(得分:1)

您需要在方法执行中从其超级视图中删除MBProgressHUD对象。 像这样:

  

//如果方法在一秒内运行,则指标不会   示

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

//如果方法在一秒后运行,指标将是   显示一段时间,直到方法运行

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