2

我正在使用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;

我已经[[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantPast]];提出了这个问题的解决方案, 请指导我这段代码有什么问题..?!

4

1 回答 1

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];
}
于 2013-08-02T04:58:37.843 回答