My usual practice is to determine whether it is in use, depending on the life cycle of the view controller.
For example, set an instance of Boolean variable isVisible in-viewwillappear isVisible = YES; Inside the-viewwilldisappear isVisible = NO; Then implement the method according to the condition
A smart way to share a recent colleague is as follows: http://edsioon.me/if-uiviewcontroller-is-display/
Determine if Uiviewcontroller is being displayed
April 18, 2014 Edsioon Experience Summary
In some cases, you need to determine whether the current Viewcontroller is showing, such as the background network request error, we may only want to launch the requested page pop-up prompts the user, when the user has jumped to another interface, do not pop windows, reduce the user interference.
If a UIView object is currently being displayed, its Window property must be a non-null value. Although the official documentation does not describe the value of the Window property when UIView is not shown, the value of window is empty for most cases when UIView is not displayed, so the current Uiviewcontroller is being displayed, based on a simple test. However, accessing the View property of Uiviewcontroller may cause the view to load (if it is not loaded at this time), which is unnecessary and may cause unexpected problems. Therefore, it is a good idea to check the Isviewloaded property before accessing the View property to avoid this problem.
The sample code is as follows, declaring a IsVisible method in the Uiviewcontroller extension to make it easier to call later
Note: The Window property is empty when the view is not added to the window in the official document of UIView, but does not mention that the current view is not displayed when window is nil. The above method can be used to meet most requirements through a simple demo, so a simple test is recommended when using it!
Instantly feel good low haha, but pay attention to the practical conditions of the method OH!!! View controllers that are not handled under special circumstances are generally available.
I wrote a class method in order to call more convenient haha
As follows:
+ (BOOL) iscurrentviewcontrollervisible: (Uiviewcontroller *) viewcontroller{ return ( viewcontroller.isviewloaded && ViewController.view.window);}
I would like to distinguish between the view of life week to judge and this tall method to compare which is more real? Tomorrow's the answer.
IOS determines whether the current Uiviewcontroller is being displayed
Label:
IOS determines whether the current Uiviewcontroller is being displayed