-2

I wrote the same code as given in Apple docs but when I click on the photo the photo library doesn't come up. The tap gesture recogniser may not be working.

import UIKit

class ViewController: UIViewController, UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        nameTextField.delegate = self
    }

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

    //MARK: Actions
    @IBAction func setDefaultLabelText(sender: UIButton) {
        nameLabel.text = "default"
    }
    @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 = UIImagePickerControllerSourceType.PhotoLibrary

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

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

    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: UITextFieldDelegate
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }

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

}
kabiroberai
  • 2,930
  • 19
  • 34

1 Answers1

1

check once are you enabled the userInteraction property for your Imageview, by default it is false, we need to enable manually, do like

photoImageView.userInteractionEnabled = true

for e.g

override func viewDidLoad(){
{
    super.viewDidLoad()

    let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:Selector("imageTapped:"))
    photoImageView.userInteractionEnabled = true
    photoImageView.addGestureRecognizer(tapGestureRecognizer)
}

call your method like

func imageTapped(img: AnyObject)
{
    // Your action 
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • Error message "EXPECTED Declaration – Shalin Gadhavi Oct 11 '16 at 10:22
  • THANKS IT WORKED I did this:- – Shalin Gadhavi Oct 11 '16 at 10:27
  • override func viewDidLoad() { super.viewDidLoad() photoImageView.userInteractionEnabled = true } – Shalin Gadhavi Oct 11 '16 at 10:27
  • @ShalinGadhavi - happy to hear – Anbu.Karthik Oct 11 '16 at 10:27
  • This solved my issue with the tutorial as well, but I was still confused why the default value for userInteractionEnabled is false when viewDidLoad() is called. I had checked to enable the "User Interaction Enabled" field of the image view in the story board editor, and the value at runtime is still false... Turns out that if a parent view (stacked view in this case) has "User Interaction Enabled" set to false, it is applied to its children too: https://stackoverflow.com/questions/5887305/uiview-user-interaction-enabled-false-on-parent-but-true-on-child – ThePartyTurtle Dec 02 '20 at 20:25