【问题标题】:UIWbView multiple URL loading with UIProgressView in iPhone SDK在 iPhone SDK 中使用 UIProgressView 加载 UIWbView 多个 URL
【发布时间】:2012-03-21 12:47:26
【问题描述】:

我正在尝试创建一个 iPhone 应用程序,如下图所示:

在此我动态地将一些图像视图动态添加到 uiscroll 视图中。每个 imageview 包含一个 UIButton 和 UIProgressView。当点击每一个它应该不同的 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]);
}

但它不起作用。 有什么想法吗?

【问题讨论】:

标签: iphone ios url uiwebview uiprogressbar


【解决方案1】:

为每个 UIWebView 分配不同的 url 并将SubView UIActivityIndi​​catorView 添加到每个 UIWebView.Give 标记为每个 UIActivityIndi​​cator ..

-(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;

}

调用此操作{}方法以显示多次。

编辑

UIWebView 在正常模式下不会为您提供任何进度信息。您需要做的是首先使用 NSURLConnection 异步获取数据。当 NSURLConnection 委托方法 connection:didReceiveResponse 时,您将获取从 expectedContentLength 获得的数字并将其用作最大值。然后,在委托方法 connection:didReceiveData 中,您将使用 NSData 实例的 length 属性来告诉您已经走了多远,因此您的进度分数将是 length / maxLength ,标准化为 0 和 1 之间。

最后,您将使用数据而不是 URL 来初始化 webview(在您的连接中:didFinishLoading 委托方法)。

参考: How to use UIProgressView while loading of a UIWebView?

【讨论】:

  • 但是,实际上我想同时处理多个 url。那么如何管理委托方法呢?因为每个 webview 都会有不同的进度条控制器。
  • - (void)webViewDidFinishLoad:(UIWebView *)webView 这是委托方法,它会给你加载哪个 webView
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多