Reputation: 137
I am newbie Studying a tutorial from Apple.https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/WorkWithViewControllers.html#//apple_ref/doc/uid/TP40015214-CH6-SW1
I do everything as a guide. I understand that it is out of date. But why is Gesture Recognizer not responding to tap
I added a check mark to "User Interaction Enabled"
Tell me what to fix in this code so that the Gesture Recognizer responds to tap
//MARK: UIImagePickerControllerDelegate
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image.
photoImageView.image = selectedImage
// Dismiss the picker.
dismiss(animated: 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
present(imagePickerController, animated: true, completion: nil)
}
}
Upvotes: 0
Views: 394
Reputation: 16341
You probably don't have/lost the connection to the @IBAction
. You need to connect it once again. Find the UITapGestureRecognizer
from the storyboard and control drag to the UIViewController
subclass and create an action again, add the code there and delete the old method.
UIViewController
.@IBAction
in the ViewController
You could also do it programmatically. Here's how:
override func viewDidLoad() {
super.viewDidLoad()
tappableView.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(selectImageFromPhotoLibrary(_:)))
tappableView.addGestureRecognizer(tapGesture)
}
Upvotes: 1