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; } 
4
  • 1
    There is a category written for UIView which might help: stackoverflow.com/questions/1340434/… Commented Aug 12, 2011 at 5:33
  • 1
    Possible duplicate : Get to UIViewController from UIView on iPhone? Commented Aug 12, 2011 at 5: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. Commented Aug 12, 2011 at 8:44
  • Hmm, might want to look into UIView tags, could help Commented Aug 12, 2011 at 13:51

3 Answers 3

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 } 
Sign up to request clarification or add additional context in comments.

Comments

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; } } 

Comments

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

2 Comments

[aView isDescendantOfView:viewController.view]
didn't see that in the headers till now :) Thanks for pointing it out

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.