如何检查特定的UIViewController视图当前是否可见? [英] How to check if a specific UIViewController's view is currently visible?
问题描述
我正在开发一个应用程序来处理来自网络的持续传入数据流,并为用户提供了许多不同的UIView来查看该数据。
I'm developing an app that processes a constant stream of incoming data from the network and provides a number of different UIViews for the user to view that data.
当基于来自网络的传入流更新某些模型数据时,我访问关联的UIViewController或UITableViewController并在其上执行-setNeedsDisplay(在UIViewController的情况下)或-reloadData(在UITableViewController的情况下)。
When certain model data gets updated based on the incoming stream from the network, I access the associated UIViewController or UITableViewController and do -setNeedsDisplay on it (in the case of UIViewController) or -reloadData (in the case of UITableViewController).
有没有办法检查当前是否正在显示给定的UIView(除了刚被加载),这样我只做--setNeedsDisplay或-reloadData如果用户当前正在查看UIView的?看起来在用户当前没有看到的视图上调用-setNeedsDisplay或reloadData会浪费处理能力,并且不利于电池寿命。当用户最终切换到之前已更新的视图时,在-viewWillAppear上执行-setNeedsDisplay或reloadData会更有意义。
Is there a way to check if a given UIView is currently being displayed (beyond just being loaded) so that I only do -setNeedsDisplay or -reloadData if the user is currently looking at that UIView? It would seem that calling -setNeedsDisplay or reloadData on a view that the user is not currently looking at is a waste of processing power and wouldn't be good for battery life. When the user eventually switches over to a view that previously got updated, doing -setNeedsDisplay or reloadData on the -viewWillAppear would make more sense.
谢谢
推荐答案
在做了一些研究之后,我在这里发布的另一个问题中找到了这个答案......这似乎是最好的方式...
After doing some research, I found this answer in a different question posted on here...This seems to be the best way...
如果视图当前可见,则视图的窗口属性为非零,因此请检查视图控制器中的主视图:
The view's window property is non-nil if a view is currently visible, so check the main view in the view controller:
if (viewController.isViewLoaded && viewController.view.window){
// viewController is visible
}
这篇关于如何检查特定的UIViewController视图当前是否可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!