tags:

views:

4361

answers:

3

I need to detect which view is in front (currently visible). How can I do this?

Here is a what I would like to do:

if ( ! <<methodToTellIfViewAIsInFront>>) {
  [viewA prepareToDisplay];
  [window bringSubviewToFront: viewA];
}
+1  A: 

The only way to do it is to assign a unique identifier to your view using [UIView tag] and then use [UIView viewWithTag] to bring it to the front.

Or you can search for the view you need using the tag and work on it..

for (UIView *checkView in [self.view subviews] ) {
      if ([checkView tag] == whatever) {
      // Do Whatever you need to do
      }
}

..then bring it to front.

Cheers

Jordan
I was not clear in my original question. I have revised it.
Andrew Raphael
+2  A: 

UIView's don't necessarily have a concept of being in front. UIWindows can be key or not, but it's not quite the same thing.

You can bring a view to the front, but that doesn't mean it is or is not visible. Remember, views can be any size.

A UIView buried deep in the hierarchy could be partially visible, it could be obscured, or it could be behind some translucent view. Likewise a view at the front may not be visible at all if its opacity value or hidden flags are modified.

I think what you want to do is check the subviews NSArray of your superview or UIWindow and check that. I can't remember which is the front, but it is either the first or last object.

Subviews are drawn with the painter's method. The views are drawn in order from farthest to nearest and the last object drawn is "the front."

Kailoa Kadano
For UIViews, the subview at index 0 of the subviews array is the one farthest to the back, with the other subviews being drawn in order on top of that one.
Brad Larson
I was debugging to examine the subviews and did find that the last subview is the one in front.
Andrew Raphael
Thanks for the followup Brad, Andrew.
Kailoa Kadano
A: 

Add this to view controller:

- (void)viewDidAppear:(BOOL)animated {
 [super viewDidAppear:animated];
 visible = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
 visible = NO;
 [super viewWillDisappear:animated];
}

and check 'visible' ivar

dimzzy