如何检查当前是否显示UIViewController?

如何检查当前是否显示UIViewController

我的UIViewControllers正在监听NSNotifications – 即使他们没有显示(即没有显示)。 所以我可以有10个UIViewController在后台观察来自NSNotificationCenter 。 当一个NSNotificationUIViewController发布和接收,我想知道它是否正在显示。 如果不是的话,我只需要设置一个布尔值,这样就可以在视图呈现的时候处理它。 如果它现在正在显示,我会做更多的事情,如立即更新表,等等…

您需要检查您的viewcontroller是否在navigationcontroller的viewcontroller数组堆栈的顶部。 示例代码是,

 if (self.navigationController.topViewController == self) { //the view is currently displayed } 

你可以在viewWillAppear方法中使用这个来检查当前视图是否可见。

检查它是否连接到窗口。 如果它不是nil则它被挂在屏幕上的层次结构中(当然,它可能超出了屏幕边界,被其他视图覆盖,或者设置了隐藏标志)

 if (myViewController.view.window) { // I'm attached to the window } else { // not attached to the window } 

你可以在viewWillAppearviewWillDisappear方法中使用标志。

你为什么不移除viewWillDisappear中的通知侦听器并将其添加到viewWillAppear中?

编辑:误解他的问题,对不起。

build议的答案:在viewDidDisappear和viewDidAppear中设置自己的标志(BOOL)。

为每个ViewController指定标题,然后按照下面的代码获取当前ViewController的标题。

 NSString *currentController = self.navigationController.visibleViewController.title; Then check it by your title like this if([currentController isEqualToString:@"myViewControllerTitle"]){ //write your code according to View controller. } 

我认为viewController.view.superview检查应该工作。