Dan Dan - 4 months ago 20
iOS Question

How do I determine the superview of UIImageView in iOS

I want to determine what view contains the image clicked on by the user in iOS 7. Below is my layout.

+-------------------------------+
| A |
| +---------------------------+ |
| |B | |
| |+--+ +--+| |
| ||C | ------------------|D || |
| |+--+ +--+| |
| | | | | |
| | | G | | |
| | | | | |
| |+--+ +--+| |
| ||E | ------------------|F || |
| |+--+ +--+| |
| +---------------------------+ |
+-------------------------------+

Legend:
A - UIView
B - UIView subview of A
C, D, E, F - UIImageView subviews of B
G - UIImageView subview of A


I did a dump of the whole window hierarchy and it correctly shows C, D, E, F as subviews of B and B, G as a subviews of A. This whole view hierarchy was built in IB and loaded as a XIB.

In the case the user tapped on the UIImageView C, I want to find out it's superview B. However, when I do self.superview inside of the hittest for class C, it's always nil.

How do I determine the UIImageView (C, D, E, F) superview?

In case you're wondering, I'm trying to create grips (C, D, E, F) to manipulate the image in G. I have multiple instances of A on the screen at a time and need to know which one is the superview of the grip that was tapped.

I'm missing some concept here and appreciate your tutelage.

Update: Here's the IB hierarchy of the views:

enter image description here

Here's the actual xib:

enter image description here

Here's the view dump at the class C hittest breakpoint:

UIWindow:<UIWindow: 0xb174590
| UILayoutContainerView:<UILayoutContainerView: 0xb17a7f0
| | UINavigationTransitionView:<UINavigationTransitionView: 0xb17c160
| | | UIViewControllerWrapperView:<UIViewControllerWrapperView: 0xb18f640
| | | | CanvasBackgroundView:<CanvasBackgroundView: 0xa63be10
| | | | | DragView:<DragView: 0xa6aec60
| | | | | | BorderView:<BorderView: 0xa6b92c0
| | | | | | | GripView:<GripView: 0xa6b2480
| | | | | | | GripView:<GripView: 0xa6bb020
| | | | | | | GripView:<GripView: 0xa6b29e0
| | | | | | | GripView:<GripView: 0xa6bb0d0
| | | | | | ContentImageView:<ContentImageView: 0xa6bb340


@Timothy, all my hitTest methods are like this:

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
DDLogVerbose(@"%@ superview: %@", NSStringFromClass([self class]), NSStringFromClass([[self superview] class]));
UIView *hitView = [super hitTest:point withEvent:event];
if (self == hitView) {
DDLogVerbose(@"hit for me: %@ %@ %@ self:%@", NSStringFromCGPoint(point), NSStringFromClass([hitView class]), NSStringFromCGRect(hitView.frame), NSStringFromCGRect(self.frame));
return self; // we handled it
} else {
DDLogVerbose(@"hit not for me: %@", NSStringFromCGPoint(point));
return nil; // not for us
}
}

Answer

Try:

 UIView *iv = [imageView superView];
Comments