我正在尝试将MBProgressHUD与NSURLConnection一起使用,但我不确定如何在MBProgressHUDModeDeterminate
中旋转动画。
基本上我在viewDidLoad中有这个代码:
- (void)viewDidLoad
{
[super viewDidLoad];
[self fetchFromServer];
self.HUD = [ [MBProgressHUD alloc] initWithView:self.view];
self.HUD.labelText = @"Loading Schedule...";
[self.view addSubview:self.HUD];
self.HUD.dimBackground = YES;
[self.HUD show:YES];
}
加载此视图时,我调用一个从数据库中获取的方法。我想加载MBProgressHUD。然后我想在获取数据后结束它。
- (void)fetchFromServer
{
NSURL *url = [[NSURL alloc] initWithString:@"url.com"];
[NSURLConnection sendAsynchronousRequest:[ [NSURLRequest alloc] initWithURL:url] queue:[ [NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSString* str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSArray *jsonObj = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding]
options:0 error:&error];
scheduleJSONArray = jsonObj;
// Progress in HUD should end here after scheduleJSONArray is set
[self.HUD hide:YES];
} ];
}
我不知道用什么来更新HUD的进度。有人可以帮忙吗?
由于
答案 0 :(得分:1)
您最好使用 AFNetworking :
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
// Set up HUD
__weak ViewController *weakSelf = self;
NSURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:@"url.com" parameters:nil error:nil];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
__weak AFHTTPRequestOperation *weakOperation = operation;
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
[weakSelf updateHUDForOpetation:weakOperation
totalBytes:totalBytesExpectedToRead
readBytes:totalBytesRead
index:[videoNames indexOfObject:videoName]
total:videoNames.count];
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
[MBProgressHUD hideHUDForView:self.view animated:YES];
}];
然后手动更新HUD。像这样:
- (void)updateHUDTotalBytes:(long long)totalBytes
readBytes:(long long)readBytes {
MBProgressHUD *HUD = [MBProgressHUD HUDForView:self.view];
HUD.mode = MBProgressHUDModeDeterminate;
HUD.labelText = [NSString stringWithFormat:@"%lld/%lld", readBytes / 1024, totalBytes / 1024];
HUD.progress = (double)readBytes / (double)totalBytes;
}