2015-02-12 27 views
1

我想用NSURLConnection使用MBProgressHUD,但我不知道如何使动画旋转MBProgressHUDModeDeterminate如何在使用NSURLConnection时计算MBProgressHUD模式的进度?

基本上我有这样的代码在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的进度。任何人都可以协助

感谢

+1

是的。你必须自己添加进度。这不是自主的 – soulshined 2015-02-12 03:17:55

回答

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; 
} 
+0

对不起,但我完全不理解代码,第一部分。我如何在当前实现中使用此代码?可能吗?谢谢 – Pangu 2015-02-14 09:34:42

+0

@Pangu,你需要使用AFNetworking框架来使用渐进式下载功能。去谷歌上查询。 – orkenstein 2015-02-14 12:20:14

+0

@orkenstein你上面的评论听起来好像是强制性的,这是错误的。你不需要使用AFNetworking框架,它可以像OP那样用'NSURLConnection'完成,但你必须添加委托和进度字节(你在字节例子中暗示的东西)。你的意见可能会让未来的问题解决者感到困惑,并且几乎没有编码经验 – soulshined 2015-08-21 12:54:42