如何判断UIViewController的视图是否可见 [英] How to tell if UIViewController's view is visible

查看:440
本文介绍了如何判断UIViewController的视图是否可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个tabbar应用程序,有很多视图。有没有办法知道UIViewController中当前是否可以看到特定的UIViewController? (寻找房产)

I have a tabbar 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)

推荐答案

视图的窗口属性如果视图是非零的目前可见,请检查视图控制器中的主视图:

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
}

或者,如果您有管理视图控制器的UINavigationController,您可以检查其 visibleViewController 属性。

Or if you have a UINavigationController managing the view controllers, you could check its visibleViewController property instead.

此外,在iOS 9上的Swift中(或者稍后):

Also, in Swift on iOS 9 (or later):

if viewController.viewIfLoaded?.window != nil {
    // viewController is visible
}

这篇关于如何判断UIViewController的视图是否可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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