我正在慢慢地通过Apple的学习开发Swift来解决问题,并且我的点击手势一直在遇到问题。我重新创建项目编号的次数都是相同的。
我向图像视图添加了一个轻击手势,这应该会从我的计算机中打开照片库。没有任何事情发生。
当我从Apple下载并运行示例文件时,一切正常。当我将Apple的代码复制并粘贴到我的代码时,没有任何反应。我已经完成了我能做的一切,但觉得我错过了一些东西。
这是我的代码。 Apple的代码如下:
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"
}
}
以下是Apple的代码:
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"
}
}
安布的工作守则
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"
}
}
答案 0 :(得分:5)
我发现解决问题的最简单方法是在图像视图的属性编辑器中单击“启用用户交互”复选框。 Screenshot
答案 1 :(得分:3)
喜欢
原因,默认情况下UIImageview userInteraction为false,因此您需要手动启用
<强>步骤1 强>
let tapGesture = UITapGestureRecognizer(target:self, action:Selector("imagePressed"))
photoImageView.userInteractionEnabled = true // this line is important
photoImageView.addGestureRecognizer(tapGesture)
<强>步骤-2 强>
func imagePressed()
{
//do Your action here whatever you want
}
答案 2 :(得分:2)
你的代码非常好。问题在于故事板中的UIImageView。未选中123456
0
poo
1
123456
printing done
选项。
所以你只需在storyboard中启用此选项。
答案 3 :(得分:0)
您在哪里为图片视图添加了UITapGestureRecognizer
?如果您在故事板中添加了它,是否已将其连接到选择器selectImageFromPhotoLibrary:
?