UIProgressView
을 만들었습니다. 그러나 나는 NSTimer
을 UIProgressView'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];
}
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
didReceiveResponse 및 didReceiveData는 2 명의 다른 대리자입니다. 그것은 있어야합니다 : - (void) connection : (NSURLConnection *) connection didReceiveResponse : (NSURLResponse *) response { totalFileSize = response.expectedContentLength; } 그 라인을 didReceiveData에서 제거하십시오. – Hless
내 대답을 편집했습니다. 내가 나누는 값 중 하나는 실수로 캐스팅되어야한다는 사실을 잊어 버렸습니다. 그렇지 않으면 float 정밀도없이 정수로 끝날 수 있기 때문입니다. – Hless