Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITAPPgestureRecogener不工作_Ios_Swift_Uitapgesturerecognizer - Fatal编程技术网

Ios UITAPPgestureRecogener不工作

Ios UITAPPgestureRecogener不工作,ios,swift,uitapgesturerecognizer,Ios,Swift,Uitapgesturerecognizer,我写的代码与Apple docs中给出的代码相同,但当我点击照片时,照片库不会出现。轻触手势识别器可能不工作 import UIKit class ViewController: UIViewController, UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate { override func viewDidLoad() { super.viewDid

我写的代码与Apple docs中给出的代码相同,但当我点击照片时,照片库不会出现。轻触手势识别器可能不工作

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
    }

}

检查一旦您为Imageview启用了userInteraction属性,默认情况下它是
false
,我们需要手动启用,执行类似操作

photoImageView.userInteractionEnabled = true
例如

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

    let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:Selector("imageTapped:"))
    photoImageView.userInteractionEnabled = true
    photoImageView.addGestureRecognizer(tapGestureRecognizer)
}
把你的方法称为

func imageTapped(img: AnyObject)
{
    // Your action 
}

在你添加手势的地方,你能展示一下代码吗?请试着提出一个问题标题来描述这个问题。避免使用诸如“紧急”之类的词,因为它们在这里不太受欢迎。好的,很抱歉这是我第一次收到错误消息“预期声明谢谢它起作用我这样做了:-覆盖函数viewDidLoad(){super.viewDidLoad()photoImageView.userInteractionEnabled=true}@ShalinGadhavi-很高兴他也解决了我在教程中遇到的问题,但我仍然不明白为什么调用viewDidLoad()时userInteractionEnabled的默认值为false。我已选中启用“已启用用户交互”“故事板编辑器中图像视图的字段,并且运行时的值仍然为false…结果表明,如果父视图(在本例中为堆叠视图)的“用户交互启用”设置为false,则它也将应用于其子视图: