7

I have a bunch of subViews in my ViewController.

In the last layer I have a UIView, and from this view I want to call superview and go up until I find the UIView that belongs to my ViewController.

Is it possible to find out whether a UIView belongs to a ViewController or not?

UIView *someView = self.superView;

while (true)
{
   if (someView BELONGS TO VIEWCONTROLLER)
   {
      // Now we know this view belongs to a VIewController
      break;
   }

   someView = someView.superView;
}
aryaxt
  • 76,198
  • 92
  • 293
  • 442
  • 1
    There is a category written for UIView which might help: http://stackoverflow.com/questions/1340434/get-to-uiviewcontroller-from-uiview-on-iphone/3732812#3732812 – Jeremy Flores Aug 12 '11 at 05:33
  • 1
    Possible duplicate : [Get to UIViewController from UIView on iPhone?](http://stackoverflow.com/questions/1340434/get-to-uiviewcontroller-from-uiview-on-iphone) – Seb T. Aug 12 '11 at 05:36
  • Not a duplicate. This question is about finding the view that belongs to a view controller, not finding the view controller for a given view. – August Lilleaas Aug 12 '11 at 08:44
  • Hmm, might want to look into UIView tags, could help – smdvlpr Aug 12 '11 at 13:51

3 Answers3

9

If you want to find out if a certain view is in the hierarchy managed by a view controller and you have a pointer to the view controller:

BOOL belongsToController = [aView isDescendantOfView:viewController.view];

Alternatively, if you want to find out if a certain view is the root of the hierarchy managed by the view controller but you don't have a pointer to the view controller, you can traverse the responder chain. According to the UIResponder's nextResponder documentation:

UIView implements this method by returning the UIViewController object that manages it (if it has one) or its superview (if it doesn’t)

Therefore, if the next responder of a certain view is a UIViewController, that view must be the view associated with the view controller.

if ([[aView nextResponder] isKindOfClass:[UIViewController class]]) {
    // aView is the root of the view hierarchy managed by the view controller
}
albertamg
  • 28,492
  • 6
  • 64
  • 71
4

Vlad's and albertamg's approaches are correct as well. However you can also traverse the responder chain

  for (UIView* next = [self superview]; next; next = next.superview) {
    UIResponder* nextResponder = [next nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]]) {
      UIViewController *theControllerThatYouWANT = (UIViewController*)nextResponder;
    }
  }
Abhinit
  • 1,016
  • 7
  • 13
3

try going up in the hierarchy of views and check if current view object is the same as your controller's view

Code would be something like this: (wrote in textEdit, don't have dev tools here, sorry if any mistakes)

-(BOOL)view:(UIView *)aView belongsToController:(UIViewController *)viewController {
  BOOL belongsToController = NO;
  UIView *someView = [aView superView];
  while (someView != nil) {
    if (viewController.view == someView) {
        belongsToController = YES;
    }
    someView = [someView superView];
  }
  return belongsToController;
}

just tested it and it works for me. I hope it was helpfull. Vlad

Vlad
  • 3,346
  • 2
  • 27
  • 39