返回 UIViewController 时不升级进度条

Posted

技术标签:

【中文标题】返回 UIViewController 时不升级进度条【英文标题】:Does not upgrade the progress bar when coming back in UIViewController 【发布时间】:2015-10-07 11:49:30 【问题描述】:

我正在通过下载管理类执行下载,并根据委托连接 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;
    

【问题讨论】:

你能贴出你的代码吗? 我得到了完美的百分比值,下载速度但不显示在 UILabel 中 你的代码是在块内调用的吗? 你看到使用 NSLog 的正确值了吗? 当我点击下载按钮时它工作正常但是当返回并再次进入 UIViewController 下载仍在运行因为我看到下载管理器接收数据方法连续调用 【参考方案1】:
-(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;
        
    

【讨论】:

以上是关于返回 UIViewController 时不升级进度条的主要内容,如果未能解决你的问题,请参考以下文章

UIViewController 关闭时不接收 dealloc 或 viewDidUnload

iOS 8 保存升级时不消失的文件

XL Deploy插件命令在升级时不执行

WiX 安装程序在卸载时删除文件,但在升级时不删除

在 MVC 4.0 站点中升级 jQuery 1.9 时不显眼的 jQuery 失败

使用yum更新时不升级Linux内核的方法