0
私のビューに付随するラベル付きプログレスバーがあり、ファイルのダウンロードの進行状況を表示しようとしています。ここまでは私のコードですCocoa:プログレスバーのみ最後から2番目の値に更新します
-(void)downloadXML {
if(responseData == nil) {
responseData = [[NSMutableData alloc] init];
}
progressView.progress = 0;
NSString* updateURL = [NSString stringWithFormat:@"http://www.myserver.com/file.xml"];
responseData = [[NSMutableData alloc] init];
NSURLRequest* updateRequest = [NSURLRequest requestWithURL: [NSURL URLWithString:updateURL]];
NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:updateRequest delegate:self];
[connection start];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
filesize = [[NSNumber numberWithLong: [response expectedContentLength] ] retain];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
NSNumber* curLength = [NSNumber numberWithLong:[responseData length] ];
float progress = [curLength floatValue]/[filesize floatValue] ;
NSString *labelText = [NSString stringWithFormat:@"Downloading file: %@%", domicile, progress];
progressView.progress = progress;
progressLabel.text = labelText;
NSLog(@"File Size: %f, Download Progress: %f", [filesize floatValue], progress);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
progressView.progress = 0;
[filesize release];
[connection release];
}
実際のダウンロードはうまくいきますが、プログレスバーとラベルはうまく動作していないようです。コンソールの出力は、ファイルが正しくダウンロードされていることを示しています。
2010-11-18 15:46:51.141 Fund Prices[8096:207] File Size: 1300.000000, Download Progress: 0.318615
2010-11-18 15:46:51.141 Fund Prices[8096:207] File Size: 1300.000000, Download Progress: 0.427615
2010-11-18 15:46:51.141 Fund Prices[8096:207] File Size: 1300.000000, Download Progress: 0.651215
2010-11-18 15:46:51.274 Fund Prices[8096:207] File Size: 1300.000000, Download Progress: 1.000000
ただし、プログレスバーとラベルは、最後から2番目の値にのみ更新されます。すなわち、バーは半分以上に進み、ラベルは0.651215に更新されます。
最終値が両方の項目に送信されない理由はありますか?
私は[NSThread sleepForTimeInterval:5]を入れました。 didFinishLoadingメソッドの行は、何も変更されませんでした。面白いのは、正しい値がコンソールには出力されているが、ラベルには出力されていないことです。 – Typhoon101
誰にもアイデアはありますか? – Typhoon101