Pregunta

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];
     }
   }
¿Fue útil?

Solución

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:.

Otros consejos

You can always make your images touchable by checking User Interaction Enabled from Attribute Inspector of your storyboard/xib.

Once you have checked that box you need to add a tap gesture recognizer to your xib/storyboard.

Then select the image view and connect its gesture recognizer outlet in Connections Inspector to that tap gesture recognizer. Also connect the delegate for the tap gesture recognizer to some class. Then you can implement your tap gesture delegate methods in that class and act accordingly there on image tap. May be you should invoke a performSegueWithIdentifier: call from the delegate method to invoke the required segue based on image tag.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top