使用点击手势识别器时Xcode错误

时间:2016-12-13 10:24:35

标签: ios swift

我开始关注Apple提供的Swift教程。在一步中使用轻敲手势识别器。点按图片时,应链接到photoLibrary。但相反,以下消息显示:

enter image description here

我通过官方回答使用了示例代码,弹出同样的问题。所以我猜这不是代码问题?

3 个答案:

答案 0 :(得分:0)

试试这个

let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
tap.delegate = self
myView.addGestureRecognizer(tap)

然后,您应该实现处理程序,每次发生点击事件时都会调用该处理程序:

func handleTap(sender: UITapGestureRecognizer? = nil) {
    // handling code
}

因此,现在调用您的点击手势识别器事件处理程序就像调用方法handleTap()

一样简单

答案 1 :(得分:0)

不确定,但如果错误是因为点按手势。你可以尝试:

let enableTap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))

view.addGestureRecognizer(enableTap)

view.userInteractionEnabled = true

 func handleTap(_ sender: UITapGestureRecognizer) {
     print("Success!!!")
  }

答案 2 :(得分:0)

我的代码:(对不起,评论回复太长了)

导入UIKit

类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
}

/*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 the user canceled.
    dismiss(animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
    // The info dictionary may contain multiple representations of the image. You want to use the original.
    guard let selectedImage = info[UIImagePickerControllerOriginalImage] 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 setDefaultLabelText(_ sender: UIButton)
{
    mealNameLabel.text = "Default Text"
}


@IBAction func selectImageFromPhotoLibrary(_ sender: Any)
{
    // 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
    // The type of imagePickerController.sourceType is known to be UIImagePickerControllerSourceType, which is an enumeration. This means you can write its value as the abbreviated form .photoLibrary instead of UIImagePickerControllerSourceType.photoLibrary.

    // Make sure ViewController is notified when the user picks an image.
    imagePickerController.delegate = self
    present(imagePickerController, animated: true, completion: nil)

}

}