I’m working on an app which dynamically pulls the images from the images folder and displays them in a ScrollView, very similar to this tutorial code from Apple (look at the viewDidLoad method):
Every image gets put into an ImageView. Now I want to make every picture clickable, but since it’s dynamic I can’t create a fixed amount of IBActions + it would be very repetitive and copy/paste, which I want to avoid.
So basically this code:
-(IBAction)Fork {
[buttonImageView setImage:[UIImage imageNamed:@"Fork.png"]];
}
but for every image that gets created. I’m especially confused because I can’t ‘connect’ the IBActions to the button through the Storyboard.
I appreciate any help with code hints since I’m a beginner.
You probably want the click to be handled by a single action and pass the image view as the sender where you can differentiate your action accordingly.
You can use a
UITapGestureRecognizerto get started quickly:Implement the handler of the tap like:
If you need to do something like have it adjust the image when you tap it you’ll probably want to try a
UIButtonwith the custom type and set the image accordingly. Anyways, hope this helps!