2011-10-13 41 views
0

我正在嘗試正確地將MBProgressHUD集成到項目中,而我正在下載和處理一些數據。原諒我的無知,如果我要求愚蠢的事情,但我是一個完整的小白菜...MBProgressHUD與兩個類的集成

我有一個「數據」類處理我的HTTPRequests,所以我這裏的東西是適當的地方放一些東西在:

-(void)resolve 
{ 
    // Create the request. 
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[[NSURL alloc] initWithString:[self url]] 
               cachePolicy:NSURLRequestUseProtocolCachePolicy 
              timeoutInterval:60.0]; 
    NSLog(@"Resolving content from: %@",[self url]); 
    // create the connection with the request 
    // and start loading the data 
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
    if (theConnection) { 
     // Create the NSMutableData to hold the received data. 
     // receivedData is an instance variable declared elsewhere. 
     receivedData = [[NSMutableData data] retain]; 
    } else { 
     NSLog(@"Content - resolve: connection failed"); 
    } 

    // Here is the part that it makes me crazy... 

    HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain]; 

    return; 

} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    // This method is called when the server has determined that it 
    // has enough information to create the NSURLResponse. 

    // It can be called multiple times, for example in the case of a 
    // redirect, so each time we reset the data. 

    // receivedData is an instance variable declared elsewhere. 
    expectedLength = [response expectedContentLength]; 
    currentLength = 0; 
    HUD.mode = MBProgressHUDModeDeterminate; 
    [receivedData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    currentLength += [data length]; 
    HUD.progress = currentLength/(float)expectedLength; 

    // Append the new data to receivedData. 
    // receivedData is an instance variable declared elsewhere. 

    [receivedData appendData:data]; 
    return; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    [HUD hide:YES]; 
    // release the connection, and the data object 
    [connection release]; 
    // receivedData is declared as a method instance elsewhere 
    [receivedData release]; 

    // inform the user 
    NSLog(@"Content - Connection failed! Error - %@ %@", 
      [error localizedDescription], 
      [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); 
    return; 
} 

現在我知道,把「showHUDaddedTo」中的 - (空)的決心是不正確的做法...

我打電話從視圖控制器這個數據功能與IBAction爲像這樣:

-(IBAction) btnClicked:(id) sender { 
    if ([[issue status] intValue] == 1) // issue is not downloaded 
    { 

     [(Content *)[issue content] resolve]; 
     //HUD = [[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES] retain]; 




    } 
    else // issue is downloaded - needs to be archived 
    { 
     NSError * error = nil; 
     [[NSFileManager defaultManager] removeItemAtPath:[(Content *)[issue content] path] error:&error]; 
     if (error) { 
      // implement error handling 

     } 
     else { 
      Content * c = (Content *)[issue content]; 
      [c setPath:@""]; 
      [issue setStatus:[NSNumber numberWithInt:1]]; 
      [buttonView setTitle:@"Download" forState:UIControlStateNormal]; 
      [gotoIssue setTitle:@"..." forState:UIControlStateNormal]; 


      // notify all interested parties of the archived content 
      [[NSNotificationCenter defaultCenter] postNotificationName:@"contentArchived" object:self]; // make sure its persisted! 

     } 



    } 
} 

簡單地說:我想從我的Conten.m文件中調用所有MBProgressHUD的東西,當我按下我的IssueController.m文件中的下載按鈕時。我想你現在看到我的問題在哪裏:我不是一個編碼器;-)任何幫助表示讚賞。

乾杯,

桑德爾

回答

0

我會嘗試使用HUD方法showWhileExecuting。用新方法包裝你的電話,並打電話下載你的信息。當你的方法結束時,你的HUD也會如此。

另外,是否有一個原因,你保留你的呼叫HUD?我之前沒有看到過,而且我不確定是否需要保留那些自動發佈的版本。

+0

thnks比爾,我會盡力做你的建議。 Reatining:我正在使用這個初始文檔中的代碼片段以及它們與HUD項目一起出現的示例「HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];」 - 正如我所說:我不是一個編碼器,所以也許我有時做愚蠢的事情:-) –

+0

不,這絕對是一種方式來做到這一點。我發現它與-showWhileExecuting相比有點難度,但它們都可以工作。希望你能按照你需要的方式工作。 –