如何检查当前是否正在显示 UIViewController? [英] How do I check if an UIViewController is currently being displayed?

查看:22
本文介绍了如何检查当前是否正在显示 UIViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I check if an UIViewController is currently being displayed?

我的 UIViewControllers 正在监听 NSNotifications - 即使它们没有显示(即没有显示).所以我可以有 10 个 UIViewController 在后台观察 NSNotifications 来自 NSNotificationCenter.当 NSNotificationUIViewController 发布和接收时,我想知道它当前是否正在显示.如果不是,我将只设置一个布尔值,以便在呈现视图时进行处理.如果它当前正在显示,我会做更多的事情,比如立即更新表等等......

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...

推荐答案

您需要检查您的视图控制器是否位于导航控制器的视图控制器数组的堆栈顶部.示例代码是,

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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