简体   繁体   English

使用iPhone SDK中的UIProgressView加载UIWbView多个URL

[英]UIWbView multiple URL loading with UIProgressView in iPhone SDK

I am trying to create an iPhone app as in the fig below: 我正在尝试创建一个iPhone应用程序,如下图所示:

在此处输入图片说明

In this i am dynamically adding some imageviews dynamically into a uiscroll view. 在这种情况下,我正在将一些图像视图动态添加到uiscroll视图中。 Each imageview contains a UIButton and UIProgressView. 每个imageview包含一个UIButton和UIProgressView。 When click on each it should different urls and the loading of each url should appear on the corresponding UIProgressView. 当单击每个URL时,应该有不同的URL,并且每个URL的加载应出现在相应的UIProgressView上。 I am using asynchronous method and it should load multiple progress views at the same time. 我正在使用异步方法,它应该同时加载多个进度视图。 Here is the code i have used, 这是我使用的代码,

- (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]);
}

But its not working. 但是它不起作用。 Any idea? 任何想法?

allocate each UIWebView with different url and and addSubView UIActivityIndicatorView to every UIWebView.Give tag for each UIActivityIndicator .. 为每个UIWebView分配不同的URL,并为每个UIActivityIndi​​cator的每个UIWebView.Give标签添加SubView UIActivityIndi​​catorView。

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

}

call this action{} method for showing multiple times. 调用此action {}方法可显示多次。

EDIT 编辑

UIWebView doesn't give you any progress information in the normal mode. 在正常模式下,UIWebView不会为您提供任何进度信息。 What you need to do is first fetch your data asynchronously using an NSURLConnection. 您需要做的是首先使用NSURLConnection异步获取数据。 When the NSURLConnection delegate method c onnection:didReceiveResponse , you're going to take the number you get from expectedContentLength and use that as your max value. 当NSURLConnection委托方法connection:didReceiveResponse时 ,将采用从ExpectedContentLength获得的数字并将其用作最大值。 Then, inside the delegate method connection:didReceiveData, you're going to use the length property of the NSData instance to tell you how far along you are, so your progress fraction will be length / maxLength , normalized to between 0 and 1. 然后,在委托方法connection:didReceiveData内,您将使用NSData实例的length属性来告诉您您走了多远,因此您的进度分数将是length / maxLength,归一化为0到1之间。

Finally, you're going to init the webview with data instead of a URL (in your connection:didFinishLoading delegate method). 最后,您将使用数据而不是URL初始化Webview(在您的connection:didFinishLoading委托方法中)。

Reference : How to use UIProgressView while loading of a UIWebView? 参考: 如何在加载UIWebView时使用UIProgressView?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM