从其他ViewController获取下载进度 [英] Get download progress from different ViewController

查看:94
本文介绍了从其他ViewController获取下载进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 VideoListController 。它具有带下载按钮的视频列表。当我按下下载按钮时,控件将转移到 DetailViewController 。在 DetailViewController 中,我正在使用 AFNetworking 下载文件。

I have a VideoListController. It has list of videos with download button. When I press the download button, the control is transferred to DetailViewController. In DetailViewController, I am using AFNetworking to download file.

如果我从 DetailViewController 回到 VideoListController 。我怎么知道下载进度或何时从 VideoListController 下载完成。我想知道这一点,因为基于此,我将重新加载列表以显示播放按钮而不是下载。

If I go back to VideoListController from DetailViewController. How can I know progress of download or when download gets completed from VideoListController. I want to know this because based upon that I will reload the list to show play button instead of download.

推荐答案

使用NSNotificationCenter工作。
在DetailViewController的viewDidLoad中,我添加了这个

I got it working using NSNotificationCenter. In viewDidLoad of DetailViewController, I added this

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(movieDownloadDidFinish)
                                             name:@"MovieDownloadDidFinishNotification"
                                           object:nil];

下载完成时。我这样称呼:

When download gets complete. I call this:

[[NSNotificationCenter defaultCenter] postNotificationName:@"MovieDownloadDidFinishNotification" object:self];

当单击导航控制器中的后退按钮时,我从DetailViewController中移除了观察者

I remove the observer from DetailViewController when when backbutton in navigation controller is clicked

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"MovieDownloadDidFinishNotification" object:nil];

并在DetailViewController中添加了下载完成时调用的方法。

And added method in DetailViewController that is called when download gets completed.

-(void) movieDownloadDidFinish {
    NSLog(@"MovieDownloadDidFinish on DetailViewController");
}

现在在VideoListController的viewDidAppear中,我添加了观察者

Now in viewDidAppear of VideoListController, I added the observer

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(movieDownloadDidFinish)
                                             name:@"MovieDownloadDidFinishNotification"
                                           object:nil];

然后在VideoListController的viewDidDisappear中,删除观察者

And in viewDidDisappear Of VideoListController, I remove the observer

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"MovieDownloadDidFinishNotification" object:nil];

在VideoListController中添加了下载完成时调用的方法。

And added method in VideoListController that is called when download gets completed.

-(void) movieDownloadDidFinish {
    NSLog(@"MovieDownloadDidFinish On VideoListController");
}

通过这种方式,当DetailViewController可见时,调用DetailViewController的movieDownloadDidFinish方法当VideoListController可见时,也会调用VideoListController的movieDownloadDidFinish。

In this way, when DetailViewController is visible, the method movieDownloadDidFinish of DetailViewController is called and similarly movieDownloadDidFinish of VideoListController is called when VideoListController is visible.

这篇关于从其他ViewController获取下载进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆