2010-11-23 4 views
5

UIProgressView을 만들었습니다. 그러나 나는 NSTimerUIProgressView's 프로세스에 사용했다. 이제 URL이로드 될 때 UIProgressView 프로세스를 통합해야합니다. UIProgressView's 크기는 NSURLConnection's 데이터에 따라 다릅니다.UIProgressView에 NSURLConnection로드 프로세스 추가

NSURLConnection에 다음 코드를 사용했습니다. _totalFileSize = response.expectedContentLength; : 당신의 didReceiveResponse 기능에서

-(void)load { 
    NSURL *myURL = [NSURL URLWithString:@"http://feeds.epicurious.com/newrecipes"]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL 
                  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
                 timeoutInterval:60]; 

    [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
} 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    responseData = [[NSMutableData alloc] init]; 
} 


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    [responseData appendData:data]; 
} 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    [connection release]; 

    UIAlertView *alert = [[UIAlertView alloc] init]; 
    [alert setTitle:@"Warning"]; 
    [alert setMessage:@"Network Connection Failed?"]; 
    [alert setDelegate:self]; 
    [alert addButtonWithTitle:@"Yes"]; 

    [alert show]; 
    [alert release]; 

    NSLog(@"Error"); 
} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    responsetext = [[[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding] autorelease]; 
} 

답변

15

당신은 전체 파일 크기과 같이 얻을 수 있습니다. 하나 MyProgressBar.progress = _receivedDataBytes/(float)_totalFileSize

(: 당신이 간단하게 할 수있는 올바른 크기로 진행 막대를 설정하기 위해 지금 _receivedDataBytes += [data length];

: 당신의 didReceiveData에서

는 다음 총 바이트받은 카운터를 추가 할 수 있습니다 기능 didReceiveData 함수 또는 다른 코드에서)

클래스에 바이트 수를 포함하는 변수를 추가하는 것을 잊지 마십시오!

난이 도움이되기를 바랍니다

..

편집 : 여기 progressview이

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    _totalFileSize = response.expectedContentLength; 
    responseData = [[NSMutableData alloc] init]; 
} 


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    _receivedDataBytes += [data length]; 
    MyProgressBar.progress = _receivedDataBytes/(float)_totalFileSize; 
    [responseData appendData:data]; 
} 
+0

didReceiveResponse에 응답을 선언하는 방법, 다음 코드를 수정하십시오 업데이트하기 위해 대표단을 구현할 수있는 방법입니다. - (void) connection : (NSURLConnection *) connection didReceiveData : (NSData *) data { \t totalFileSize = response.expectedContentLength; \t receivedDataBytes + = [데이터 길이]; \t NSLog (@ "totalFileSize/totalFileSize = % f", receivedDataBytes/totalFileSize); \t [responseData appendData : data]; } – Velmurugan

+0

didReceiveResponse 및 didReceiveData는 2 명의 다른 대리자입니다. 그것은 있어야합니다 : - (void) connection : (NSURLConnection *) connection didReceiveResponse : (NSURLResponse *) response { totalFileSize = response.expectedContentLength; } 그 라인을 didReceiveData에서 제거하십시오. – Hless

+0

내 대답을 편집했습니다. 내가 나누는 값 중 하나는 실수로 캐스팅되어야한다는 사실을 잊어 버렸습니다. 그렇지 않으면 float 정밀도없이 정수로 끝날 수 있기 때문입니다. – Hless

관련 문제