Cocoa-touch – How to access subviews in nested UIScrollViews

cocoa-touchsubviewuiimageviewuiscrollview

I was having trouble getting a UIScrollView with multiple UIImageView subviews to zoom properly so I tried using a UIScrollView with multiple UIScrollView subviews inside of it and a UIImageView inside of each of those. That way one UIScrollView can only scroll and the other can only zoom.

My question is: How can I access subviews of a subview. I know that normally I can access subviews using [scrollView viewWithTag:tagInt]; for example, but I can't seem to access the subviews of a subview using [[scrollView viewWithTag:tagInt] viewWithTag:tagInt2]; since viewWithTag only returns a single UIView and not all of it's subviews.

I could always give each subview a unique tag and access them that way, but that doesn't seem to be the most elegant solution.

What is the best way to access a subView and then get to the subView's subview (ie: access my UIScrollView used for zooming which is a subview of my main view, and then access it's subView UIImageView)?

Best Answer

If you don't feel comfortable subclassing for now, you'd have to assign tags to the UIScrollViews containing images. You'd then have to do what the BobC suggested with a little more work.

for (UIView *subview in [myScrollView subviews])
{
   //check if the current subview is one of the UIScrollViews
   if (subview.tag > 100)
        //do something with the UIScrollView
}
Related Topic