Thomas Florin
Thomas Florin

Reputation: 403

UITapGestureRecognizer Action on UIImageView in swift

I am having the hardest time trying to figure out what is wrong with my UITapGestureRecognizer, I have placed breakpoints at the beginning of the action, and it never gets called. I am following the example shown here.

https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson4.html

And I have the UIImageView wired up to the tap gesture like this.

UIImageView Setup And then I have the UITapGestureRecognizer set up like this.

UITapGestureRecognizer setup

Lastly my code is almost an exact replica of the code from the demo.

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate{

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



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

}

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

//MARK: UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder();
    return true;
}


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

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    // Dismiss the picker if the user canceled.
    dismissViewControllerAnimated(true, completion: nil)
}
 // MARK: Actions
@IBAction func selectImageFromLibraryAlso(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)
}

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.
    imagePhotoControl.image = selectedImage

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


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

I just don't understand why the action would never get fired, when I tap the image. I even downloaded the demo code, and that works as intended. I did name my project Food Tracker with a space, could that be the problem? I am basically driving myself insane trying to figure out why the event doesn't get fired. I am about ready to do a folder diff between my project, and the demo project.

ANSWER CONT.: So it turns out there is a checkbox on the UIImageView in the Attributes Inspector called User Interaction Enabled. If you check this it will allow touch events to fire from your UITapGestureRecognizer. Another solution is to do it in code as stated in the answer below.

Upvotes: 1

Views: 2057

Answers (1)

Muhammad Adnan
Muhammad Adnan

Reputation: 2668

//set in ViewDidLoad

imagePhotoControl.userInteractionEnabled = true ;

Upvotes: 2

Related Questions