Reputation: 4313
How do I check if an UIViewController
is currently being displayed?
My UIViewControllers
are listening for NSNotifications
- even if when they are not displayed (ie not shown). So I could have 10 UIViewController
in the background observing NSNotifications
from NSNotificationCenter
. When an NSNotification
is posted and received by the UIViewController
, I'd like to find out if it is currently being shown. If it is not, I will just set a boolean so that it will processed when the View is presented. If it currently being display, I will do more things like update tables immediately, and so forth...
Upvotes: 22
Views: 26771
Reputation: 34263
One more alternative which is based on checking window
property
if viewController.viewIfLoaded?.window != nil {
// visible
}
Upvotes: 0
Reputation: 1555
It's too late to replay on this question.
To check the instance of a UIViewController
is currently on the top of the screen or to check if it is showing on screen, you can put a check like:
// Get the topmost view showing on the screen as below
UIViewController * currentVC = ((UINavigationController*)app.window.rootViewController).visibleViewController;
// Now check whether the viewcontroller you want to show is the same as the currently showing view controller.
if (currentVC.class == myVC.class) { // Here myVC is any/new instance of the viewcontroller you would like to check or show (if not shown).
// If both are same then it returns true and executes this block of code.
}
Upvotes: 0
Reputation: 241
Specify title to each ViewController and then get the title of current ViewController by the code given bellow.
NSString *currentController = self.navigationController.visibleViewController.title;
Then check it by your title like this
if([currentController isEqualToString:@"myViewControllerTitle"]){
//write your code according to View controller.
}
Upvotes: 1
Reputation: 23278
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.
Upvotes: 21
Reputation:
You can use flags in viewWillAppear
and viewWillDisappear
methods for this.
Upvotes: 3
Reputation: 23233
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
}
Upvotes: 12
Reputation: 3092
Why don't you remove the notification listener in viewWillDisappear and add it in viewWillAppear?
Edit: misread his question, sorry.
Suggested answer: set your own flag (BOOL) in viewDidDisappear and viewDidAppear.
Upvotes: 1
Reputation: 641
I think that checking of viewController.view.superview
should works.
Upvotes: 0