Sélecteur rapide non reconnu envoyé à une erreur d'instance

J'ai récemment converti mon projet d'Objective-C à Swift et, ce faisant, j'ai acquis cette erreur chaque fois que je clique sur un button dans la cellule de la table. J'ai plusieurs cellules remplies d'informations provenant d'un server mysql. J'ai deux buttons, un button suivi et un button suivi, quand on est cliqué l'autre est censé montrer. J'ai travaillé dessus pendant un moment mais j'ai été bloqué sur cette erreur.

Erreur que je reçois lorsque je clique sur le button dans la vue de table

CustomCellSwift[1425:372289] -[CustomCellSwift.ViewController followButtonClick:]: unrecognized selector sent to instance 0x100b13a40 

Dans CustomCell.swift

 class CustomCell: UITableViewCell { @IBOutlet weak var firstStatusLabel: UILabel! @IBOutlet weak var secondStatusLabel: UILabel! @IBOutlet weak var myImageView: UIImageView! @IBOutlet weak var followButton: UIButton! @IBOutlet weak var followedButton: UIButton! override func awakeFromNib() { super.awakeFromNib() self.followButton.isHidden = true self.followedButton.isHidden = true } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } func populateCell(_ testObject: Test, isFollowed: Bool, indexPath: IndexPath, parentView: Any) { // Loading Background Color self.backgroundColor = UIColor.white // Loading Status Labels self.firstStatusLabel.text = testObject.testStatus1 self.secondStatusLabel.text = testObject.testStatus2 self.firstStatusLabel.isHidden = true self.secondStatusLabel.isHidden = true if isFollowed { self.followedButton.tag = indexPath.row self.followedButton.addTarget(parentView, action: Selector(("followedButtonClick")), for: .touchUpInside) self.followedButton.isHidden = false self.followButton.isHidden = true // Status Labels self.firstStatusLabel.isHidden = false self.secondStatusLabel.isHidden = false } else { self.followButton.tag = indexPath.row self.followButton.addTarget(parentView, action: Selector(("followButtonClick:")), for: .touchUpInside) self.followedButton.isHidden = true self.followButton.isHidden = false // Status Labels self.firstStatusLabel.isHidden = false // True when done testing self.secondStatusLabel.isHidden = false // True when done testing } } } 

ViewController.swift

CellForRowAt indexPath

 public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let CellIdentifier = "Cell" var cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! CustomCell if cell != cell { cell = CustomCell(style: UITableViewCellStyle.default, reuseIdentifier: CellIdentifier) } // Coloring TableView myTableView.backgroundColor = UIColor.white // Configuring the cell var testObject: Test if !isFiltered { if indexPath.section == 0 { testObject = followedArray[indexPath.row] cell.populateCell(testObject, isFollowed: true, indexPath: indexPath, parentView: self) } else if indexPath.section == 1 { testObject = testArray[indexPath.row] cell.populateCell(testObject, isFollowed: false, indexPath: indexPath, parentView: self) } } else { testObject = filteredArray[indexPath.row] as! Test cell.populateCell(testObject, isFollowed: false, indexPath: indexPath, parentView: self) } return cell } 

Suivre le code du button

 @IBAction func followButtonClick(sender: UIButton!) { // Adding row to tag let buttonPosition = (sender as AnyObject).convert(CGPoint.zero, to: self.myTableView) if let indexPath = self.myTableView.indexPathForRow(at: buttonPosition) { // Showing Status Labels let cell = self.myTableView.cellForRow(at: indexPath) as! CustomCell cell.firstStatusLabel.isHidden = false cell.secondStatusLabel.isHidden = false // Change Follow to Following (sender as UIButton).setImage(UIImage(named: "follow.png")!, for: .normal) cell.followButton.isHidden = true cell.followedButton.isHidden = false self.myTableView.beginUpdates() // ----- Inserting Cell to Section 0 ----- followedArray.insert(testArray[indexPath.row], at: 0) myTableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .fade) // ----- Removing Cell from Section 1 ----- testArray.remove(at: indexPath.row) let rowToRemove = indexPath.row self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 1)], with: .fade) self.myTableView.endUpdates() } } 

Le code du button Ne plus suivre est le même que le button Suivre.

Je pense que le problème est dans CustomCell.swift dans le sélecteur de button (("")) mais l'erreur dit – [CustomCellSwift.ViewController followButtonClick:] ce qui signifie dans ViewController dans le code de button suivi, mais je ne sais pas quoi faire plus.

Deux changements pour Swift 3 :

Le sélecteur devrait ressembler à:

 #selector(ClassName.followButtonClick(_:)) 

La fonction devrait avoir un trait de soulignement:

 @IBAction func followButtonClick(_ sender: UIButton!) { ... 

Notez que ces deux doivent être dans la même class, sinon, assurez-vous d'initialiser la class ClassName .

Si vous souhaitez que la méthode de sélection ( followButtonClick(_:) ) figure dans la class UITableViewCell . Supprimer @IBAction (je ne pense pas que vous en ayez besoin ici):

 func followButtonClick(_ sender: UIButton!) { ... 

Pour swift 2.2 avec Xcode8

 self.followedButton.addTarget(parentView, action: #selector(CustomCell.followButtonClick(_:)), forControlEvents: .TouchUpInside) 

Pour Swift3 , vous devez modifier les éléments suivants:

 self.followedButton.addTarget(parentView, action: Selector(("followedButtonClick")), for: .touchUpInside) 

Avec:

 self.followedButton.addTarget(parentView, action: #selector(self.followButtonClick(_:)), forControlEvents: .touchUpInside)