Search code examples
iossegueuistoryboardsegue

IOS - Segue - detect which image was clicked


i am working on IOS. I am using images instead of buttons.there is a segue between one controller to another. so depending on which image has been clicked i want to load specific information in my SecondController. so I want to know is that possible to detect which image has been tapped and then pass the image name or title or whatever i can detect to another controller using Segue.The other way is two have a transparent buttons on the images and then get the Tag value. but i want to know if somehow i can detect which image clicked then why go for an extra code.

   -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
   {
        NSInteger tagIndex = [(UIButton *)sender tag];
   if (([segue.identifier isEqualToString:@"rightImageSegue"]))
     {

        SecondViewController *destViewController = segue.destinationViewController;
        [destViewController setSelectedButton:tagIndex];
     }
   }

Solution

  • There are a couple of ways to do this. First of all, UIButtons can actually have a background image, which you could set to your various images. If you're intent on using a UIImageView, you would have to implement its touch event receiving method and handle it yourself. Either way, you can use performSegueWithIdentifier:.