回到UIViewController

时间:2015-10-07 11:49:30

标签: ios objective-c uilabel uiprogressbar

我正在通过Download Manage类执行下载,并根据委托连接didReceiveData中收到的字节更新进度条视图。

当我在正在进行下载的页面上时,一切正常,但是当我要访问其他视图控制器并返回到我的下载页面时,连接委托功能在页面转换时被调用但是回来了不升级进度条。

这就是我的代码

for (NSInteger row = 0; row < [downloadManager.downloads count]; row++)
{
    if (download == downloadManager.downloads[row])
    {
        currentTime = [NSDate timeIntervalSinceReferenceDate];

        [self updateProgressViewForIndexPath:[NSIndexPath indexPathForRow:row inSection:0] download:download];

lbl_percentage.text = [NSString stringWithFormat:@"%.02f%%",((double) download.progressContentLength / (double) download.expectedContentLength)*100];

        lbl_received_data.text = [NSString stringWithFormat:@"%@ / %@",[self transformedValue:(double) download.progressContentLength],[self transformedValue:(double) download.expectedContentLength]];

        double downloadSpeed = (double) download.progressContentLength / (currentTime - startTime);

        lbl_speed.text = [NSString stringWithFormat:@"%@/sec", [self transformedValue:downloadSpeed]];

        NSLog(@"Download Running");

        break;
    }
}

1 个答案:

答案 0 :(得分:0)

-(void)viewDidAppear:(BOOL)animated
{
    [[self downloadManager] setDelegate:self];
}

-(IBAction)btn_Download_click:(id)sender
{
    if ([self downloadManager]) {
        [[self downloadManager] setDelegate:self];
    }else{
        self.downloadManager = [[DownloadManager alloc] initWithDelegate:self];
        self.downloadManager.maxConcurrentDownloads = 1;
    }

    downloadData = [[NSMutableData data] retain];

    NSLog(@"coverimg%@",coverimg);

    NSString *string=[[NSString stringWithFormat:@"http://www.virtueinfotech.com/moralstory/pdf/%@",coverimg] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *reqURL = [NSURL URLWithString:string];

    startTime = [NSDate timeIntervalSinceReferenceDate];

    [self Readfunction];
    [self.downloadManager addDownloadWithFilename:filePath URL:reqURL];

}
-(IBAction)btn_Read_click:(id)sender
{
    [self Readfunction];

    PdfReadView *pdfRv = [[PdfReadView alloc]init];
    [self.navigationController pushViewController:pdfRv animated:YES];
}

#pragma mark - Download Manager Delegate Methods

- (void)updateProgressViewForIndexPath:(NSIndexPath *)indexPath download:(Download *)download
{
    if (download.expectedContentLength >= 0)
    {
        // Calculate the progress.
        dispatch_async(dispatch_get_main_queue(), ^{

            [self setProgress:(double) download.progressContentLength / (double) download.expectedContentLength animated:YES];

            lbl_percentage.text = [NSString stringWithFormat:@"%.02f%%",((double) download.progressContentLength / (double) download.expectedContentLength)*100];

            lbl_received_data.text = [NSString stringWithFormat:@"%@ / %@",[self transformedValue:(double) download.progressContentLength],[self transformedValue:(double) download.expectedContentLength]];

            double downloadSpeed = (double) download.progressContentLength / (currentTime - startTime);

            lbl_speed.text = [NSString stringWithFormat:@"%@/sec", [self transformedValue:downloadSpeed]];
        });
    }
    else
    {
        [self setProgress:(double) (download.progressContentLength % 1000000L) / 1000000.0 animated:YES];
    }
}
- (void)downloadManager:(DownloadManager *)downloadManager downloadDidFail:(Download *)download;
{
    [self setProgress:0.0f animated:YES];
    UIAlertView *alt = [[UIAlertView alloc] initWithTitle:@"Oops !!" message:@"Downloading Failed...." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alt show];
}

- (void)downloadManager:(DownloadManager *)downloadManager downloadDidReceiveData:(Download *)download;
{

    lbl_total_size.text = [NSString stringWithFormat:@"%@",[self transformedValue:(double) download.expectedContentLength]];

    for (NSInteger row = 0; row < [downloadManager.downloads count]; row++)
    {
        if (download == downloadManager.downloads[row])
        {
            currentTime = [NSDate timeIntervalSinceReferenceDate];
            dispatch_async(dispatch_get_main_queue(), ^{

                [self updateProgressViewForIndexPath:[NSIndexPath indexPathForRow:row inSection:0] download:download];
            });
            NSLog(@"Download Running");

            break;
        }
    }
}