0

I have a UIViewController in which I added a UIView. In that UIView I added some UIImageViews through programming. What I need is that when I click the image it shows in a new UIViewController.

ViewController : UIViewController {
    myUIview[[imageview]  [ ]  [  ]  [  ]  [  ] ]
}
Blackwood
  • 4,504
  • 16
  • 32
  • 41
Saad Ullah
  • 103
  • 2
  • 13

1 Answers1

0

If I understand correctly you want to segue to another ViewController when your users press on an UIImageView. So here is my solution using an UITapGestureRecognizer (put this code in viewDidLoad):

yourImageView.isUserInteractionEnabled = true
let yourImageTapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.test(_ :)))
yourImageTapGesture.delegate = self
yourImageTapGesture.numberOfTapsRequired = 1
yourImageView.addGestureRecognizer(yourImageTapGesture)

Then in your ViewController you have to make the function test, which is triggered when somebody taps on your UIImageView:

func test(_ sender: AnyObject) {
    self.performSegue(withIdentifier: "YourSegueName",sender: self)
}

Important: "YourSegueName" is the Identifier of your Segue in your Storyboard. You can set it by clicking on your Segue:

enter image description here

Keep in my mind that your ViewController needs to inherit from one more class:

class ViewController: UIViewController, UIGestureRecognizerDelegate {...}
tech4242
  • 2,348
  • 2
  • 23
  • 33
  • Thank you so much for your explanation but you misunderstood one part. I have multiple imageviews in my uiview. so what i want is that when i click first image in my uiview then image show up in new view controller. Keep in mind that i have made imageViews through programming. – Saad Ullah Aug 25 '16 at 18:26
  • @SaadUllah I suggest you pass the UIImageView with the segue. Keep my code and add the following: http://stackoverflow.com/a/25215574/6597361 – tech4242 Aug 25 '16 at 18:31
  • Is your code able to identify that which image i want to pick. Consider I have 4 images in my UIView – Saad Ullah Aug 25 '16 at 18:44
  • Add something like `for image in yourUIImageViewArray { ... }` and add a variable there with the right image, which should be passed to the other VC – tech4242 Aug 25 '16 at 18:52