Suppose I have a subclass of UIViewController (MyViewController) which has in it’s own view, a subclass of UIImageView (MyImageView), and a standard UILabel.
In the code for MyImageView, I am doing some work on the touch events to get the color of a the pixel being touched, and want to update my the UILabel with that contents. What are some typical ways achieve this communication since MyImageView and UILabel both reside as instances in MyViewController?
Coming from C++ I would have added something like this to MyImageView
-(void)registerMyLabel:(UILabel*)label;
Then somewhere in the initialization of MyViewController would have registered the UILabel instance with MyImageView using the method above. I am sure that I can use a similar technique here, but I’m wondering… is there a built in mechanism to do this with UI controls in objective-c?
How would you structure this?
I would make the view controller do the work of communicating between views.
Whether the touch handling is the view’s responsibility or that of the controller is subject to different philosophies but let’s say we define the view as having color detecting ability. Once the color is detected,
MyImageViewshould let the controller know either by using a delegate protocol or by posting aNSNotification. Once given that information, the controller does whatever it needs to with the label.(I like notifications better than delegates in many cases because of the looser coupling.)
In any case, it’s more in keeping with MVC if the controller is responsible for communicating events and data among the other pieces that need it.