2010-11-23 1 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

_receivedDataBytes += [data length];

今、あなたは簡単に行うことができます正しいサイズにプログレスバーを設定するために: MyProgressBar.progress = _receivedDataBytes/(float)_totalFileSize

(いずれかごdidReceiveDataで

あなたはその後、合計バイト数は、カウンターを受けまで追加することができます機能しますdidReceiveData関数またはコード内の他の部分)

クラスにバイト数を保持する変数を追加するのを忘れないでください!

私は

EDIT ..このことができます願っています:ここでは、didReceiveResponseで応答を宣言するためにどのように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

を更新するために、デリゲートを実装する方法ですが、次のコードを編集してください。 - (void)接続:(NSURLConnection *)接続didReceiveData:(NSData *)データ{ \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

私の答えを編集しました。あなたが割り切れる値の1つが浮動小数点にキャストされるべきだと言いました。そうでなければ、浮動小数点精度のない整数で終わる可能性があるからです。 – Hless

関連する問題