2011-10-13 3 views
0

일부 데이터를 다운로드하고 처리하는 동안 프로젝트에서 MBProgressHUD를 올바르게 통합하려고합니다. 내 바보 같은 것들을 묻는다면 내 무지를 용서하지만, 나는 완전한 멍청 아. ...두 클래스로 MBProgressHUD 통합

나는 나의 HTTPRequests를 다루는 "Data"클래스를 가지고있다. 그래서 나는 여기에 몇 가지 물건을 넣기위한 적당한 장소이다. 에 - 내가 IBAction를 가진 뷰 컨트롤러에서이 데이터 함수를 호출하고

(무효) 해결 올바른 방법으로되지 않습니다

-(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) 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! 

     } 



    } 
} 

단순히 : 내 IssueController.m 파일에서 다운로드 버튼을 누르는 순간 내 Conten.m 파일에서 모든 MBProgressHUD 항목을 호출하고 싶습니다. 나는 내 문제가 어디에 있는지 당신이 지금 생각한다 : 나는 코더가 아니다 ;-) 어떤 도움을 주셔서 감사합니다.

건배,

산도르

답변

0

나는 시도하고 HUD 방법 showWhileExecuting을 사용합니다. 새로운 방법으로 전화를 걸고 전화하여 정보를 다운로드하십시오. 방법이 끝나면 HUD도 끝납니다.

또한 HUD 요청을 유지하는 이유가 있습니까? 나는 전에 그것을 보지 못했고 autoreleased 될 때 그 것을 유지해야하는지 확신 할 수 없다.

+0

Thnks Bill, 내가 제안한 것을 시도 할 것입니다. Reatining : 나는 HUD 프로젝트와 함께 제공되는 초기 문서 및 예제에서이 코드 스 니펫을 사용하고있었습니다. "HUD = [[MBProgressHUD showHUDAddedTo : self.view animated : YES] retain];" - 내가 말했듯이 나는 코더가 아니기 때문에 가끔 어리석은 일을 때때로한다. :-) –

+0

아니, 그건 분명히 그것을하는 한 방법이다. 나는 그것이 -showWhileExecuting 대 조금 더 힘든 작업이라는 것을 알았지 만 둘 다 확실히 작동한다. 다행스럽게도 당신이 원하는 방식대로 작동하도록 할 수 있습니다. –