HUD在iOS中不工作

HUD在iOS中不工作,ios,objective-c,mbprogresshud,Ios,Objective C,Mbprogresshud,我需要从JSON对象加载一些图像。加载这些图像时,我需要在视图上显示HUD。我试图定义一个像下面这样的HUD 在viewDidLoad方法中定义HUD MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud.mode = MBProgressHUDModeIndeterminate; hud.labelText = @"Loading Images"; [hud show:YES]; [hud

我需要从JSON对象加载一些图像。加载这些图像时,我需要在视图上显示HUD。我试图定义一个像下面这样的HUD

在viewDidLoad方法中定义HUD

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.labelText = @"Loading Images";
[hud show:YES];
[hud showWhileExecuting:@selector(loadJSONData) onTarget:self withObject:nil animated:YES];
-(void)loadJSONData{
   _myObject = [[NSMutableArray alloc] init];

   NSData *jsonSource = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];

   id jsonObjects = [NSJSONSerialization JSONObjectWithData:
                  jsonSource options:NSJSONReadingMutableContainers error:nil];
   NSArray *dataDic = [jsonObjects objectForKey:@"data"];
   for (NSDictionary *dicData in dataDic) {
      Lawyer *l = [[Lawyer alloc] init];
      dispatch_async(queue, ^{
         NSString *imgUrl = [NSString stringWithFormat:@"myurl",l.imageUrl];
         NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgUrl]];
         UIImage *image = [UIImage imageWithData:data];
         dispatch_async(dispatch_get_main_queue(), ^{
           l.uiImage = image;
        });

     });
   [_myObject addObject:[l initFromDictionary:dicData]];
   }
}
加载JSONDATA方法

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.labelText = @"Loading Images";
[hud show:YES];
[hud showWhileExecuting:@selector(loadJSONData) onTarget:self withObject:nil animated:YES];
-(void)loadJSONData{
   _myObject = [[NSMutableArray alloc] init];

   NSData *jsonSource = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];

   id jsonObjects = [NSJSONSerialization JSONObjectWithData:
                  jsonSource options:NSJSONReadingMutableContainers error:nil];
   NSArray *dataDic = [jsonObjects objectForKey:@"data"];
   for (NSDictionary *dicData in dataDic) {
      Lawyer *l = [[Lawyer alloc] init];
      dispatch_async(queue, ^{
         NSString *imgUrl = [NSString stringWithFormat:@"myurl",l.imageUrl];
         NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgUrl]];
         UIImage *image = [UIImage imageWithData:data];
         dispatch_async(dispatch_get_main_queue(), ^{
           l.uiImage = image;
        });

     });
   [_myObject addObject:[l initFromDictionary:dicData]];
   }
}
我的out put显示HUD一秒钟后消失了。没有数据正在加载。我该如何解决这个问题

提前谢谢

使用以下代码:

MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView: self.view];
[self.view addSubview: hud];
hud.labelText = @"Loading Images...";

[hud showAnimated:YES whileExecutingBlock:^{
    [self loadJSONData];
} completionBlock:^{
    // Refresh UI 
}];

但要确保,在使用线程执行块时,您必须在主线程上更改所有UI。

您的代码按预期工作
loadJSONData
执行异步请求,使选择器实际完成执行。您应该做的是在方法
loadJSONData
之前显示HUD,然后在以下位置接收数据时将其隐藏:

dispatch_async(dispatch_get_main_queue(), ^{
   l.uiImage = image;
   [MBProgressHUD hideHUDForView:self.view animated:YES];
});

虽然我个人会在
loadJSONData
中有一个回调,并在完成块中执行HUD解除。

使用,使用MBProgressHUD要容易得多。.设置一个最小显示时间。很可能loadJSONData运行得很快。在我这样做之后,当我滚动它时,会显示图像。很抱歉,我只是从Xcode复制代码并放在那里。你必须在那里添加方法名。而且,没有加载数据。只加载空表。您向completionBlock添加了什么?让我们来看看。