2015-02-12 29 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