2016-10-23 26 views
-1

我正在尝试为我的应用程序创建注册页面。但是当我运行应用程序时出现错误。尝试使用轻击手势识别器的无法识别的选择器错误

终止应用程序由于未捕获的异常 'NSInvalidArgumentException',原因是: ' - [__ NSCFBoolean选择:]:无法识别的选择发送到实例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

的错误表明您没有正确地调用一个booleanselect(_:)功能:

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

检查你的代码后,看看在何处以及如何被称为select(_:),很明显,问题在于你将UITapGestureRecognizer的目标设置为布尔值,即true

let tap = UITapGestureRecognizer(target: true, action: #selector(SignupViewController.select(_:))) 

当它应该被设置为你的函数的视图控制器。例如,在这种情况下,你可能希望你的目标设定为self

let tap = UITapGestureRecognizer(target: self, action: #selector(SignupViewController.select(_:))) 

至于select(_:)方法你打电话,在我看来,你做了一个错字,并且你的意思是叫selectPhoto(tap:)您创建的方法;在这种情况下,您双击手势声明和初始化应改为:

let tap = UITapGestureRecognizer(target: self, 
          action: #selector(SignupViewController.selectPhoto(tap:))) 
+0

但是,当我写selectPhoto所以我得到的错误,(类型SignupViewController没有成员“selectPhoto”)。我正在使用Xcode 8和Swift 3 –

+0

好的,我解决了这个问题,只是写“tap”而不是“_”。 –

+0

感谢您的评论。 –