Testing whether a view is currently visible

When manipulating iOS UI from background threads, or in response to NSNotification messages, you won’t always be sure that the view your controller is working with is on-screen. How can you check?

Relatively easy. Here’s a simple check from within the context of a View’s ViewController:

if self.isViewLoaded() && self.view.window != nil { // do something }

This is appropriate when the “something” shouldn’t be executed unless the view is currently visible to the user.

Author: Rob Kerr

Consultant and expert in software development for the iOS and Android platforms. Microsoft MVP Alumnus.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.