2012-03-21 2 views
0

私は、以下の図のようにiPhoneアプリを作成しようとしています:私は、動的にuiscrollビューに動的にいくつかのimageviewsを追加していUIProgressViewとUIWbView複数のURLのロード

この中

enter image description here

。各画像ビューにはUIButtonとUIProgressViewが含まれています。それぞれのURLをクリックすると、異なるurlが必要になり、各urlの読み込みが対応するUIProgressViewに表示されます。私は非同期メソッドを使用しており、複数の進行状況ビューを同時にロードする必要があります。 ここに私が使用したコードがあります。

- (void)startDownload:(id)sender { 

    UIButton *btn = (UIButton *)sender; 
    int btnTag = btn.tag; 
    selectedTag = btn.tag; 

    for (int x=0; x < [urlArray count]; x++) { 
     if (btnTag == x) { 
      NSURL *url = [NSURL URLWithString:[urlArray objectAtIndex:x]]; 
      NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

      NSURLConnection *theConnection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; 

      [NSURLConnection sendAsynchronousRequest:request 
               queue:[NSOperationQueue mainQueue] 
            completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
             NSLog(@"Expected length: %lld ",response.expectedContentLength); 
            }]; 
      if (theConnection) { 
       receivedData = [NSMutableData data];      
      }else { 
       NSLog(@"Connection failed"); 
      }    
     } 
    } 
} 

- (void)makeMyProgressMove{ 

    UIImageView *image = (UIImageView *)[mainView viewWithTag:selectedTag]; 
    UIProgressView *currentProgress = (UIProgressView *)[image viewWithTag:selectedTag]; 
    NSLog(@"Prog tag: %d",currentProgress.tag); 

    if(currentProgress) 
    { 
     float actualProgress = (_receivedDataBytes/(float)_totalFileSize); 
     currentProgress.progress = actualProgress; 

    } else { 
     NSLog(@"couldn't get the progress view for the image with tag: %d", selectedTag); 
    } 
} 

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

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    _receivedDataBytes += [data length]; 

    NSLog(@"Receiving data: %2f", _receivedDataBytes/(float)_totalFileSize); 

    if ((_receivedDataBytes/(float)_totalFileSize) < 1) { 
     [self performSelectorOnMainThread: @selector(makeMyProgressMove) withObject: NULL waitUntilDone:NO]; 
    } 
    else if ((_receivedDataBytes/(float)_totalFileSize) == 1){ 
     [self performSelectorOnMainThread: @selector(makeMyProgressMove) withObject: NULL waitUntilDone:NO]; 
     _receivedDataBytes = 0; 
     _totalFileSize = 0; 
    } 
    [receivedData appendData:data]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]); 
} 

しかし、それは動作しません。

+0

[WebViewの答えでuiprogress] [1] [1]:http://stackoverflow.com/a/19173341/2314592 – Cristiana214

答えて

1

各UIActivityIndi​​cator毎UIWebView.Giveタグに異なるURLとし、addSubView UIActivityIndi​​catorViewとそれぞれのUIWebViewを割り当てる..

-(void)action { 

//Create a URL object. 
NSURL *url = [NSURL URLWithString:urlAddress]; 

//URL Request Object 
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
UIWebView *webView = [[UIWebView alloc]initWithFrame:frame];//give y value incremented frame. 

UIActivityIndicatorView *av = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease]; 
av.frame=CGRectMake(145, 160, 25, 25); 
av.center = webView.center; 
av.tag = INDICATOR_TAG; 
[webView addSubview:av]; 
webView.delegate = self; 
[av startAnimating]; 

//Load the request in the UIWebView. 
[webView loadRequest:requestObj]; 
} 

- (void)webViewDidFinishLoad:(UIWebView *)webView { 

UIActivityIndicatorView *indicator = [webView viewWithTag:INDICATOR_TAG]; 
indicator.hidden = YES; 

} 

コール複数回を示すためにこのアクション{}の方法。

EDIT

のUIWebViewはあなたの通常モードで任意の進捗情報を与えるものではありません。まず、NSURLConnectionを使用してデータを非同期に取得します。 NSURLConnectionデリゲートメソッドc onnection:didReceiveResponseの場合、expectedContentLengthから取得した数値を最大値として使用します。次に、デリゲートメソッドのconnection:didReceiveDataの内部で、NSDataインスタンスのlengthプロパティを使用して、どれだけ距離があるかを示します。したがって、進行の割合は、0から1の間で正規化されたlength/maxLengthになります。

最後に、URLではなくデータでwebviewを初期化します(接続:didFinishLoadingデリゲートメソッド)。

参考:これは助けることができる How to use UIProgressView while loading of a UIWebView?

+0

でも、実際には複数のURLを同時に処理したいと考えています。では、デリゲートメソッドをどのように管理できますか?各Webビューには異なる進行状況バーコントローラがあるためです。 – Mithuzz

+0

- (void)webViewDidFinishLoad:(UIWebView *)webViewこれはデリゲートメソッドであり、ロードされたwebView – Raj

関連する問題