How to check if a UIView is a subview of a parent view

The Crazy Chimp picture The Crazy Chimp · Oct 16, 2011 · Viewed 18.8k times · Source

I have an app which I add a subview to (and remove the same subview based on user interactions). I am looking for a way to check whether the subview is present or not at any given time.

For example:

In the current view (UIView *viewA) I add a subview (UIView *viewB). I then want a way of checking whether viewB is being displayed at any given time.

Sorry if this isn't very clear, it's quite hard to describe.

Answer

thomas picture thomas · Oct 16, 2011

an UIView stores its superview and is accessible with the superview-property just try

if([viewB superview]!=nil)
    NSLog(@"visible");
else
    NSLog(@"not visible");

But the better approach is to use the hidden-property of UIView