尝试使用点击手势识别器时无法识别的选择器错误

时间:2016-10-23 17:36:46

标签: ios swift uitapgesturerecognizer unrecognized-selector

我正在尝试为我的应用创建一个注册页面。但是当我运行应用程序时出现错误。

  

由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [__ NSCFBoolean select:]:无法识别的选择器发送到实例0x10ad5a690'

我的代码有什么问题及其含义?

这是我的代码:

import UIKit

class SignupViewController: UIViewController {

    @IBOutlet weak var profileImage: UIImageView!
    @IBOutlet weak var usernameTextField: UITextField!
    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!

    let imagePicker = UIImagePickerController()
    var selectedPhoto: UIImage!

    override func viewDidLoad() {
        super.viewDidLoad()


        let tap = UITapGestureRecognizer(target: true, action: #selector(SignupViewController.select(_:)))
           tap.numberOfTapsRequired = 1
        profileImage.addGestureRecognizer(tap)
    }

    func selectPhoto(tap:UITapGestureRecognizer) {
        self.imagePicker.delegate = self
        self.imagePicker.allowsEditing = true
        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            self.imagePicker.sourceType = .camera
        }else{
            self.imagePicker.sourceType = .photoLibrary
        }
        self.present(imagePicker, animated: true, completion: nil)
    }

    @IBAction func CancelDidTapped(_ sender: AnyObject) {
        dismiss(animated: true, completion: nil) 
    }

    @IBAction func RegisterDidTapped(_ sender: AnyObject) {
    }
}

extension SignupViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate{

    //ImagePicker

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        selectedPhoto = info[UIImagePickerControllerEditedImage] as? UIImage
        self.profileImage.image = selectedPhoto
        picker.dismiss(animated: true, completion: nil)
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        self.dismiss(animated: true, completion: nil)
    }
}

1 个答案:

答案 0 :(得分:2)

该错误表示您在select(_:)上错误地调用了boolean函数:

 '-[__NSCFBoolean select:]: unrecognized selector sent to instance 0x10ad5a690'

在检查您的代码以查看您调用select(_:)的位置和方式后,很明显问题是您将UITapGestureRecognizer的目标设置为布尔值,即{{ 1}}:

true

何时应将其设置为您的函数的视图控制器。例如,在这种情况下,您可能希望将目标设置为let tap = UITapGestureRecognizer(target: true, action: #selector(SignupViewController.select(_:)))

self

对于您正在调用的let tap = UITapGestureRecognizer(target: self, action: #selector(SignupViewController.select(_:))) 方法,在我看来,您输了一个拼写错误,并且您打算调用您创建的select(_:)方法;在这种情况下,您的点击手势声明和初始化应该是:

selectPhoto(tap:)