IOS determines whether the current Uiviewcontroller is being displayed

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.