How do I check if an UIViewController is currently being displayed? How do I check if an UIViewController is currently being displayed? ios ios

How do I check if an UIViewController is currently being displayed?


You need to check if your viewcontroller is on top of the stack of navigationcontroller's viewcontroller array. A sample code is,

if (self.navigationController.topViewController == self) {    //the view is currently displayed}

You can use this inside the viewWillAppear method to check whether the current view is visible.


Check to see if it's attached to the window. If it's not nil it's in hierarchy which is attached to the screen (of course it could be off the bounds of the screen, covered by some other view or have the hidden flag set)

if (myViewController.view.window) {  // I'm attached to the window} else {  // not attached to the window}


You can use flags in viewWillAppear and viewWillDisappear methods for this.