问题描述
如何检查当前是否显示 UIViewController
?
How do I check if an UIViewController
is currently being displayed?
我的 UIViewControllers
正在侦听 NSNotifications
- 即使它们未显示(即未显示)。所以我可以在后台观察来自 NSNotificationCenter NSNotifications
10 UIViewController
C>。当 UIViewController
发布并收到 NSNotification
时,我想知道它是否正在显示。如果不是,我将设置一个布尔值,以便在显示视图时处理它。如果它当前正在显示,我会做更多的事情,比如立即更新表等等......
My UIViewControllers
are listening for NSNotifications
- even if when they are not displayed (ie not shown). So I could have 10 UIViewController
in the background observing NSNotifications
from NSNotificationCenter
. When an NSNotification
is posted and received by the UIViewController
, I'd like to find out if it is currently being shown. If it is not, I will just set a boolean so that it will processed when the View is presented. If it currently being display, I will do more things like update tables immediately, and so forth...
推荐答案
你需要检查你的viewcontroller是否在navigationcontroller的viewcontroller数组的堆栈之上。示例代码是,
You need to check if your viewcontroller is on top of the stack of navigationcontroller's viewcontroller array. A sample code is,
if (self.navigationController.topViewController == self) {
//the view is currently displayed
}
您可以在 viewWillAppear
检查当前视图是否可见的方法。
You can use this inside the viewWillAppear
method to check whether the current view is visible.
这篇关于如何检查当前是否正在显示UIViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!