问题描述
我有一个标签栏应用程序,有很多视图.有没有办法知道特定的 UIViewController
当前是否在 UIViewController
中可见?(寻找房产)
I have a tab bar application, with many views. Is there a way to know if a particular UIViewController
is currently visible from within the UIViewController
? (looking for a property)
推荐答案
视图的 如果视图当前可见,则 window 属性 非 nil,因此请检查视图控制器中的主视图:
The view's window property is non-nil if a view is currently visible, so check the main view in the view controller:
调用 view 方法会导致视图加载(如果未加载)这是不必要的并且可能是不受欢迎的.最好先检查它是否已经加载.我已经添加了对 isViewLoaded 的调用来避免这个问题.
Invoking the view method causes the view to load (if it is not loaded) which is unnecessary and may be undesirable. It would be better to check first to see if it is already loaded. I've added the call to isViewLoaded to avoid this problem.
if (viewController.isViewLoaded && viewController.view.window) {
// viewController is visible
}
从 iOS9 开始,它变得更容易了:
Since iOS9 it has became easier:
if viewController.viewIfLoaded?.window != nil {
// viewController is visible
}
或者如果你有一个 UINavigationController 管理视图控制器,你可以检查它的 visibleViewController 属性.
这篇关于如何判断 UIViewController 的视图是否可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!