我遇到了一个问题:我使用AFNetworking从服务器获取数据,我使用NSOperationQueue为它添加了许多操作,在每个请求中,我将此操作添加到队列中并使用waitUntilAllOperationsAreFinished
如下:
request 1
...
[queue addOperation:operation1];
[queue waitUntilAllOperationsAreFinished];
request 2
...
[queue addOperation:operation2];
[queue waitUntilAllOperationsAreFinished];
我尝试了上面的代码,我的程序似乎挂起,之后,它运行正常。所以我想将MBProgressHUD添加到等待队列完成,然后我想检查队列是否完成,我想要隐藏MBProgressHUD。但是当我点击Button加载UIViewController时,MBProgressHUD无法显示。
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = @"Loading";
实际上,我想在队列完成时显示MBProgressHUD。我怎样才能做到这一点?谢谢大家
答案 0 :(得分:12)
很快就可以这样做:
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
[MBProgressHUD hideHUDForView:self.view animated:YES];
检查MBProgressHUD
的{{3}}
答案 1 :(得分: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];
和doSomeFunkyStuff
- (void)doSomeFunkyStuff {
float progress = 0.0;
while (progress < 1.0) {
progress += 0.01;
HUD.progress = progress;
usleep(50000);
}
}
答案 2 :(得分:2)
waitUntilAllOperationsAreFinished
将阻塞当前线程,这可能是主线程,所以你真的不想这样做。
如果您正在使用AFNetworking,请在AFHTTPClient
- (void)enqueueBatchOfHTTPRequestOperations:(NSArray *)operations
progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock
completionBlock:(void (^)(NSArray *operations))completionBlock;
然后告诉你HUD然后调用这个方法并在completionBlock