We're working on a project using some custom views. We have the following hierarchy:
UIViewController -> UIScrollView (custom subclass) -> UIView (custom subclass)
We are presenting a grid of buttons that are dynamically generated. When a user taps one of the UIViews that belong to the custom scroll view we fire a method that looks like this:
- (void)handleTapFrom:(UITapGestureRecognizer *)recognizer {
[[self superview] itemSelected:self];
}
The super view in this case is our custom subclass of UIScrollView. From here we fire another method:
- (void) itemSelected: (id)selectedItem {
itemView *item = selectedItem;
[[self superview] initSliderViewForItemNamed:item.name];
item = nil;
}
Now here is where things break. We want to fire another method in UIViewController to load a new view at the top of our view hierarchy. So in UIViewController we have this method to test for success:
- (void) initSliderViewForItemNamed:(NSString *)selectedItemName {
NSLog(@"Selected item was found! %@",selectedItemName);
}
But we never reach this point and the app crashes. It's because we can't reference the UIViewController here. Instead we're referencing the view property of the UIViewController. So our actual object hierarchy is:
UIViewController.view -> UIScrollView (custom subclass) -> UIView (custom subclass)
This leads me to two questions.
- How do we reference the UIViewController from our subview that belongs to the controller's view property?
- Is this method convoluted. Is there a better way to do this? Should we be assigning the the UIViewController as a delegate of our custom subclass of UIScrollView?