From within a UIViewController, I programmatically create about 10 instances of a CustomUIView and place them on the [UIViewController view].
When any of those instances of CustomUIView is touched, I want to let the controller know and take some action.
I’ve thought of two ways to do this:
-
Subscribe the UIViewController as an observer to the CustomUIView. Then when touchesBegan:withEvent: is fired, call a method on the UIViewController observer.
-
Use [self.nextResponder touchesBegan:touches withEvent:event] to raise the event to the UIViewController.
I’ve implemented both, and they both work ok.
Question: What is the best way for a programmatically-created view to communicate with its UIViewController? Is one of these the way to go? Or is there a third way?
The resources I’ve found online are very good at explaining all the Cocoa “pieces”, but I’m having trouble finding best practices for overall architecture.
For the case of touch events, you don’t have to do anything!
From the UIViewController class reference:
This means your view controller can handle touch events in its view without any extra setup, as long as you don’t handle them in your view.