I am writing a simple app where I have a UIImageView that when clicked on, should open a UIImagePickerController, and once an image is selected, is should just assign that image to the UIImageView
I started by sub-classing UIImageView, and giving it a touchesBegan which would open an ImagePicker.
My question is, since only a UIViewController can call presentModalViewController to show the imagePicker, how should I go about this?
It’s bad design to have your a view reference it’s controller (though I guess my imageview is a subview). So I don’t know how I’d pass the message up to the controller.
The other option is to detect touches in the controller to begin with, but then the only way I can see to tell if the touch was on the imageview is to actually test if the touch is within the frame of the imageview. This approach seems clunky to me… Am I missing something obvious?
Any thoughts on which method to go with, or any suggestions on a better method would be much appreciated! Thanks!
You don’t necessarily need to subclass
UIImageViewto do this. You can have it as a subview of your mainUIViewfor someUIViewController, and add aUIGestureRecognizerto theUIImageView. Then the method that tapping or swiping theUIImageViewtriggers will be in yourUIViewController, so you can easily present a modal view from there.