Parker O'Connel
Parker O'Connel

Reputation: 77

iOS - Tap Gesture issue on image view

I am slowly working my way through Apple's learn developing for Swift and I keep running into an issue with my tap gesture. I have re-created the project numbers times all with the same outcome.

I add a tap gesture to an image view and this should open the photo library from my computer. Nothing happens.

When I download and run the sample file from Apple everything works. When I copy and paste the code from Apple's to mine, again, nothing happens. I have gone through everything that I can but feel that I am missing something.

Here is my code. Apple's code is below:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    // MARK: Properties
    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var mealNameLabel: UILabel!
    @IBOutlet weak var photoImageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // handle the text fields user input through delegate callbacks
        nameTextField.delegate = self

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: UITextFieldDelegate
    func textFieldShouldReturn(textField: UITextField) -> Bool {
       // Hide the Keyboard
        textField.resignFirstResponder()
        return true

    }
    func textFieldDidEndEditing(textField: UITextField) {
        mealNameLabel.text = textField.text
    }

    // MARK: UIImagePickerControllerDelegate

    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        // dismiss the picker if user cancels
        dismissViewControllerAnimated(true, completion: nil)
    }

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        // The info dictionary contains multiple representations of the image, and this uses the original.
        let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage

        // Set the photoviewimage to be the selected image
        photoImageView.image = selectedImage

        // Dismiss the picker
        dismissViewControllerAnimated(true, completion: nil)
    }

    // MARK: Actions

    @IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) {
        // Hide the Keyboard
        nameTextField.resignFirstResponder()

        // UIImagePickerController is a view controller that lets a user pick media from their photo library.
        let imagePickerController = UIImagePickerController()

        // Only Allow pictures to be selected and not taken
        imagePickerController.sourceType = .PhotoLibrary

        // make sure the viewcontroller is notified when the user selects an image
        imagePickerController.delegate = self

        presentViewController(imagePickerController, animated: true, completion: nil)

    }

    @IBAction func setDefaultLabelText(sender: UIButton) {
        mealNameLabel.text = "Default Text"
    }

}

Here is Apple's code:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    // MARK: Properties

    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var mealNameLabel: UILabel!
    @IBOutlet weak var photoImageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Handle the text field’s user input through delegate callbacks.
        nameTextField.delegate = self
    }

    // MARK: UITextFieldDelegate

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        // Hide the keyboard.
        textField.resignFirstResponder()
       return true
    }

    func textFieldDidEndEditing(textField: UITextField) {
        mealNameLabel.text = textField.text
    }

    // MARK: UIImagePickerControllerDelegate
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        // Dismiss the picker if the user canceled.
        dismissViewControllerAnimated(true, completion: nil)
    }

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        // The info dictionary contains multiple representations of the image, and this uses the original.
        let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage

        // Set photoImageView to display the selected image.
        photoImageView.image = selectedImage

        // Dismiss the picker.
        dismissViewControllerAnimated(true, completion: nil)
    }

    // MARK: Actions
    @IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) {
        // Hide the keyboard.
        nameTextField.resignFirstResponder()

        // UIImagePickerController is a view controller that lets a user pick media from their photo library.
        let imagePickerController = UIImagePickerController()

        // Only allow photos to be picked, not taken.
        imagePickerController.sourceType = .PhotoLibrary

        // Make sure ViewController is notified when the user picks an image.
        imagePickerController.delegate = self

        presentViewController(imagePickerController, animated: true, completion: nil)
    }

    @IBAction func setDefaultLabelText(sender: UIButton) {
        mealNameLabel.text = "Default Text"
    }

}

Working Code from Anbu

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate  {


    // MARK: Properties
    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var mealNameLabel: UILabel!
    @IBOutlet weak var photoImageView: UIImageView!



    override func viewDidLoad() {
        let tapgesture = UITapGestureRecognizer(target: self, action: Selector("imagepressed"))
        photoImageView.userInteractionEnabled = true
        photoImageView.addGestureRecognizer(tapgesture)

        super.viewDidLoad()

        // handle the text fields user input through delegate callbacks
        nameTextField.delegate = self

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: UITextFieldDelegate
    func textFieldShouldReturn(textField: UITextField) -> Bool {
       // Hide the Keyboard
        textField.resignFirstResponder()
        return true

    }
    func textFieldDidEndEditing(textField: UITextField) {
        mealNameLabel.text = textField.text
    }

    // MARK: UIImagePickerControllerDelegate

    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        // dismiss the picker if user cancels
        dismissViewControllerAnimated(true, completion: nil)
    }

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        // The info dictionary contains multiple representations of the image, and this uses the original.
        let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage

        // Set the photoviewimage to be the selected image
        photoImageView.image = selectedImage

        // Dismiss the picker
        dismissViewControllerAnimated(true, completion: nil)
    }

    // MARK: Actions


    func imagepressed () {
        nameTextField.resignFirstResponder()

        // UIImagePickerController is a view controller that lets a user pick media from their photo library.
        let imagePickerController = UIImagePickerController()

        // Only Allow pictures to be selected and not taken
        imagePickerController.sourceType = .PhotoLibrary

        // make sure the viewcontroller is notified when the user selects an image
        imagePickerController.delegate = self

        presentViewController(imagePickerController, animated: true, completion: nil)
    }



/*    @IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) {
        let tapgesture = UITapGestureRecognizer(target: self, action: Selector("imagepressed"))
        photoImageView.userInteractionEnabled = true
        photoImageView.addGestureRecognizer(tapgesture)

        // Hide the Keyboard
        nameTextField.resignFirstResponder()

        // UIImagePickerController is a view controller that lets a user pick media from their photo library.
        let imagePickerController = UIImagePickerController()

        // Only Allow pictures to be selected and not taken
        imagePickerController.sourceType = .PhotoLibrary

        // make sure the viewcontroller is notified when the user selects an image
        imagePickerController.delegate = self

        presentViewController(imagePickerController, animated: true, completion: nil)

    }
   */

    @IBAction func setDefaultLabelText(sender: UIButton) {
        mealNameLabel.text = "Default Text"
    }

}

Upvotes: 2

Views: 3385

Answers (4)

Akash Bhardwaj
Akash Bhardwaj

Reputation: 328

Your code is perfectly fine. The problem is with the UIImageView in the story board. The user interaction option is unchecked.

So you just have to enable this option in storyboard.

  1. Go to your story board.

  2. Select your UIImageView (you named it photoimageview)

  3. Now in “Attribute Inspector” look for user interaction enabled, it should be “checked”

  4. Now run your project it will work.

enter image description here

Upvotes: 3

Bimba Kampli
Bimba Kampli

Reputation: 51

The easiest way I found that solved the issue is to click the checkbox that says "User Interaction Enabled" in the Attributes Editor for the Image View. Screenshot

Upvotes: 5

lostInTransit
lostInTransit

Reputation: 71047

Where have you added the UITapGestureRecognizer for the image view? If you added it in Storyboard, did you connect it to the selector selectImageFromPhotoLibrary:?

Upvotes: 0

Anbu.Karthik
Anbu.Karthik

Reputation: 82766

do like

The reason ,by default the UIImageview userInteraction is false, so you need to enable manually

Step-1

let tapGesture = UITapGestureRecognizer(target:self, action:Selector("imagePressed"))
photoImageView.userInteractionEnabled = true // this line is important
photoImageView.addGestureRecognizer(tapGesture)

Step-2

func imagePressed()
{
//do  Your action here  whatever you want 
}

Upvotes: 3

Related Questions