2

我有一个问题:我使用 AFNetworking 从服务器获取数据,我使用 NSOperationQueue 向它添加了许多操作,在每个请求中,我将此操作添加到队列中并用作waitUntilAllOperationsAreFinished如下:

request 1
...
    [queue addOperation:operation1];
    [queue waitUntilAllOperationsAreFinished];

request 2
...
    [queue addOperation:operation2];
    [queue waitUntilAllOperationsAreFinished];

我尝试了上面的代码,我的程序似乎挂起,之后,它运行正常。所以我想将 MBProgressHUD 添加到等待队列完成,然后我想检查队列是否完成,我想隐藏 MBProgressHUD。但是当我单击按钮加载 UIViewController 时,MBProgressHUD 无法显示。

HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = @"Loading";

实际上,我想在队列完成时显示 MBProgressHUD。我怎样才能做到这一点?谢谢大家

4

3 回答 3

12

很快你就可以这样做了:

[MBProgressHUD showHUDAddedTo:self.view animated:YES];
[MBProgressHUD hideHUDForView:self.view animated:YES];

检查MBProgressHUD使用情况

于 2013-10-14T02:31:16.030 回答
3

另一种更好的方法..

  HUD = [[MBProgressHUD alloc] initWithView:self.view];
    HUD.labelText = @"Doing funky stuff...";
    HUD.detailsLabelText = @"Just relax";
    HUD.mode = MBProgressHUDModeAnnularDeterminate;
    [self.view addSubview:HUD];

    [HUD showWhileExecuting:@selector(doSomeFunkyStuff) onTarget:self withObject:nil animated:YES];

做一些时髦的东西

- (void)doSomeFunkyStuff {
    float progress = 0.0;

    while (progress < 1.0) {
        progress += 0.01;
        HUD.progress = progress;
        usleep(50000);
    }
}

详细答案在这里..

于 2014-09-29T12:17:15.663 回答
2

waitUntilAllOperationsAreFinished会阻塞当前线程,这可能是主线程,所以你真的不想这样做。

如果您使用的是 AFNetworking,请查看此方法AFHTTPClient

- (void)enqueueBatchOfHTTPRequestOperations:(NSArray *)operations 
                              progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock 
                            completionBlock:(void (^)(NSArray *operations))completionBlock;

所以显示你的HUD然后调用这个方法并将你的HUD隐藏在completionBlock

于 2013-10-14T09:41:50.457 回答