如何判断 UIViewController 的视图是否可见
我有一个标签栏应用程序,有很多视图。有没有办法知道特定的 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
我用于模态呈现视图控制器的方法是检查呈现控制器的类。如果呈现的视图控制器是 ViewController2,那么我将执行一些代码。
The approach that I used for a modal presented view controller was to check the class of the presented controller. If the presented view controller was
ViewController2
then I would execute some code.如果您使用导航控制器并且只想知道您是否位于活动和最顶层控制器中,请使用:
此答案基于@mattdipasquale的评论。
如果您有更复杂的场景,请参阅上面的其他答案。
If you are using a navigation controller and just want to know if you are in the active and topmost controller, then use:
This answer is based on @mattdipasquale's comment.
If you have a more complicated scenario, see the other answers above.
您可以通过
window
属性检查它you can check it by
window
property我需要这个来检查视图控制器是否是当前查看的控制器,我通过检查是否有任何呈现的视图控制器或通过导航器推送来做到这一点,我将其发布以防有人需要这样的解决方案:
I needed this to check if the view controller is the current viewed controller, I did it via checking if there's any presented view controller or pushed through the navigator, I'm posting it in case anyone needed such a solution:
窗口:
查看 (macOS):
Window:
View (macOS):
如果视图当前可见,则视图的 window 属性 为非零,因此,请检查视图控制器中的主视图:
调用 视图 方法会导致视图加载(如果未加载),这是不必要的,而且可能是不可取的。最好先检查一下是否已经加载。我添加了对 isViewLoaded 的调用来避免这个问题。
自 iOS9 以来,它变得更加容易:
或者如果您有一个 UINavigationController 管理视图控制器,您可以检查它的 visibleViewController 属性。
The view's window property is non-nil if a view is currently visible, so check the main view in the view controller:
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.
Since iOS9 it has became easier:
Or if you have a UINavigationController managing the view controllers, you could check its visibleViewController property instead.
这是 @progrmr 作为
UIViewController
类别的解决方案:Here's @progrmr's solution as a
UIViewController
category:上述解决方案存在几个问题。例如,如果您使用的是
UISplitViewController
,则主视图将始终返回 true ,相反,请采用这种简单的方法,该方法似乎在大多数情况下(如果不是所有情况下)都能很好地工作:
There are a couple of issues with the above solutions. If you are using, for example, a
UISplitViewController
, the master view will always return true forInstead, take this simple approach which seems to work well in most, if not all cases:
对于那些正在寻找 Swift 2.2 版本答案的人:
和 Swift 3:
For those of you looking for a Swift 2.2 version of the answer:
and Swift 3:
对于超全屏或超上下文模式呈现,“可见”可能意味着它位于视图控制器堆栈的顶部,或者只是可见但被另一个视图控制器覆盖。
要检查视图控制器“是顶部视图控制器”是否与“可见”有很大不同,您应该检查视图控制器的导航控制器的视图控制器堆栈。
我写了一段代码来解决这个问题:
For over-full-screen or over-context modal presentation, "is visible" could mean it is on top of the view controller stack or just visible but covered by another view controller.
To check if the view controller "is the top view controller" is quite different from "is visible", you should check the view controller's navigation controller's view controller stack.
I wrote a piece of code to solve this problem:
您想要使用
UITabBarController
的selectedViewController
属性。附加到选项卡栏控制器的所有视图控制器都有一个tabBarController
属性集,因此您可以从任何视图控制器的代码中:You want to use the
UITabBarController
'sselectedViewController
property. All view controllers attached to a tab bar controller have atabBarController
property set, so you can, from within any of the view controllers' code:我根据@progrmr 的回答进行了快速扩展。
它允许您轻松检查
UIViewController
是否在屏幕上,如下所示:扩展名:
I made a swift extension based on @progrmr's answer.
It allows you to easily check if a
UIViewController
is on screen like so:The extension:
出于我的目的,在容器视图控制器的上下文中,我发现
效果很好。
For my purposes, in the context of a container view controller, I've found that
works well.
我在 Swift 5 中使用这个小扩展,它可以简单轻松地检查 UIView 成员的任何对象。
然后,我只是将它用作简单的 if 语句检查...
我希望它有帮助! :)
I use this small extension in Swift 5, which keeps it simple and easy to check for any object that is member of UIView.
Then, I just use it as a simple if statement check...
I hope it helps! :)
好的一点是,如果视图已经在窗口层次结构堆栈中,则会出现该视图。
因此我们可以扩展我们的类来实现此功能。
Good point that view is appeared if it's already in window hierarchy stack.
thus we can extend our classes for this functionality.
我在 UIViewController.h 中找到了这些函数。
也许上面的函数可以检测ViewController是否出现。
I found those function in
UIViewController.h
.Maybe the above functions can detect the
ViewController
is appeared or not.XCode 6.4,对于 iOS 8.4,启用了 ARC
显然有很多方法可以做到这一点。对我有用的一个是以下...
这可以通过以下方式在任何视图控制器中使用,
如果您在
-(void)viewDidLoad 中调用此属性,您将得到 0,那么如果你在
-(void)viewDidAppear:(BOOL)animated
之后调用它,你得到 1。希望这对某人有帮助。谢谢!干杯。
XCode 6.4, for iOS 8.4, ARC enabled
Obviously lots of ways of doing it. The one that has worked for me is the following...
This can be used in any view controller in the following way,
If you call this property in
-(void)viewDidLoad
you get 0, then if you call this after-(void)viewDidAppear:(BOOL)animated
you get 1.Hope this helps someone. Thanks! Cheers.
如果您正在使用 UINavigationController 并且还想处理模式视图,那么我使用以下内容:
if you're utilizing a UINavigationController and also want to handle modal views, the following is what i use: