iOS:if a ViewController is Visible

from:stackoverflow

The view's window property is non-nil if a view is currently visible, so check the main view in the view controller:

[EDIT] 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
}

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

原文地址:https://www.cnblogs.com/ihojin/p/ios-if-viewcontroller-visible.html