2016-10-23 30 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

感謝您的評論。 –