1
Closed. This question needsdebugging details. It is not currently accepting answers.

Edit the question to includedesired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.

Closed10 years ago.

I have a UIView that has:

  1. Image View
  2. Text View
  3. Scroll View (it has multiple images which are dynamically created in run time)

what I need to do is: when I select an Image (either the first image ,or the one from the scroll view) a pop up window shall appear with the image inside.

I prepared the pop up view but now all I need is a way to identify the image being pressed by the user, so that I can call the pop up controller and view the image.thx in advanced.

askedMay 28, 2015 at 18:41
Ameer Fares's user avatar
2
  • What is a pop up view for you ? A Popover?CommentedMay 28, 2015 at 19:48
  • @VictorSigler I didn't understand your question. but the pop up is an xib file that i call when needed with the right pictureCommentedMay 28, 2015 at 21:44

1 Answer1

2

Simple solution: Add tap gesture recognizer to every imageview.Then in gesture recognizer selector you can use view property of the sender, which is the view gesture is attached to.

Example:

    UIImageView* imageView1;    UITapGestureRecognizer* tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTaps:)];/* The number of fingers that must be on the screen */ tapGestureRecognizer.numberOfTouchesRequired = 1;/* The total number of taps to be performed before the gesture is recognized */tapGestureRecognizer.numberOfTapsRequired = 1;

then in handleTaps you can do the following

-(void) handleTaps:(UITapGestureRecognizer*)paramSender{   UIImageVIew* seletedImageView = paramSender.view; UIImage* image = selectImageView.image; //do whatever you want with image}

*Don't forget to set imageView.userInteractionEnabled = YES;

swift tested code

import UIKitclass ViewController: UIViewController {    override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view, typically from a nib.        var imageView1 : UIImageView        var image1 : UIImage        image1 = UIImage(named: "testImage")!        imageView1 = UIImageView(image: image1)       imageView1.tag = 1        imageView1.frame = CGRectMake(10, 10, 100, 100)        let tapGesture = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))        tapGesture.numberOfTouchesRequired = 1        tapGesture.numberOfTapsRequired = 1        imageView1.userInteractionEnabled = true        imageView1.addGestureRecognizer(tapGesture)        self.view.addSubview(imageView1)    }    func handleTap(sender: UITapGestureRecognizer) {        var imageView : UIImageView = sender.view as UIImageView        var image : UIImage = imageView.image!        println("Taped UIImageVIew"+String( imageView.tag))    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }}
answeredMay 28, 2015 at 20:21
user80755's user avatar
Sign up to request clarification or add additional context in comments.

5 Comments

this should work for me I think, but it's in Obj-C I'll try to find a similar thing in swift. But if u have the swift edition. it'll be much faster... thx
one more thing though, var imageView : UIImageView = sender.view is giving me an error. "cannot convert the expression's type UIView? to the type 'UIImageView' how can i handle this ?
can u please help me getting through the last bit of code ? I looked everywhere trying to find a solution for my error. and I can't seem to find anything, the error is: var imageView : UIImageView = sender.view is giving me an error. "cannot convert the expression's type UIView? to the type 'UIImageView'
I edited the answer to fulfill your needs :)
thx man, u saved my day and thx for being patient with me

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.