0

I'm trying to add a new subView to a page, such that everything but itself is greyed out. However, I'm calling it from within a subview of the screen. To get it out, I have to do the following :

[self.view.superview.superview.superview.superview addSubview:self.cardDialog.view];

As you can surmise, this is extremely bad code. How can I find the proper parent level and set it correctly?

gran_profaci
  • 8,087
  • 15
  • 66
  • 99

2 Answers2

6

If the view is part of the view hierarchy, use the window property.

UIView* topView = self.view.window;

Or if your view is not on screen yet, you can get the window indirectly through your app delegate

UIView* topView = [UIApplication sharedApplication].delegate.window;

Else, if your target is not the window, you can walk up the view hierarchy until you find the view you want:

UIView* topView = self.view;

while(topView.superview != nil){
    topview = topView.superview;

    if( /*topview is the one you were looking for*/ ){
        break;
    }
}
mspensieri
  • 3,501
  • 15
  • 18
0

I've found myself on this situation once and I gave up. I decided to use a modalView instead with a cross disolve transition. Not only you avoid the problem you are having but it helps keeping things organized. Lately I improved this aproach by grabing a snapshot of the parentVC and sending it to the modalView, then I apply a tint. The overall effect is exactly what you are looking for, I guess.

Marcal
  • 1,371
  • 5
  • 19
  • 37