我如何知道哪个ViewController是活动的? [英] How can I know which ViewController is active?

查看:147
本文介绍了我如何知道哪个ViewController是活动的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用NSOperationQueue下载图片。



我要调用我的视图控制器的一个方法 reloadView 图像下载完成。



但是,当下载正在进行时,很可能用户已移动到其他视图。此另一个视图还将具有 reloadView 方法(例如,第一个视图显示已下载的图片总数,第二个显示下载图片的缩略图)



基本上我想要的是,每当图像下载完成,我应该能够调用活动视图控制器的 reloadView 方法? / p>

这怎么可能?

解决方案

这种方法。这是 NSNotificationCenter 的设计用途。当您的图片完成下载后,请张贴通知。在视图需要知道它的控制器中,监听 viewDidAppear:中的通知并停止在 viewDidDisappear:中监听。您的下载代码不需要知道您的视图控制器的详细信息或其状态。


I am downloading images using NSOperationQueue.

I want to call a method reloadView of my view controller once the image download is complete.

However when the download is in progress, it is fairly possible that user has moved to a different view. This other view will also have a reloadView method (e.g. first view shows total downloaded images count, and second shows thumbnails of download images)

Basically what I want is that whenever an image download is completed, I should be able to call the reloadView method of the active view controller whichever it is?

How can this be possible?

解决方案

I wouldn't take that approach. This is the kind of thing NSNotificationCenter is designed for. When your image has finished downloading, post a notification. In view controllers that need to know about it, listen for the notification in viewDidAppear: and stop listening in viewDidDisappear:. Your downloading code doesn't need to know the details of your view controllers or their status.

这篇关于我如何知道哪个ViewController是活动的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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