Swift无法识别的select器发送到实例错误

我最近把我的项目从Objective-C转换到了Swift,并且这样做只要点击表格视图单元格中的一个button就会收到这个错误。 我有多个单元格正在填充从MySQL服务器的信息。 我有两个button,一个button和后面的button,当一个被点击时,另一个应该显示。 我一直在这个工作了一段时间,但我一直卡在这个错误。

当我点击tableview中的button时出现错误

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

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

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() } } 

取消追踪button代码与追随button相同。

我认为问题是在buttonselect器((“”))CustomCell.swift,但错误是说 – [CustomCellSwift.ViewController followButtonClick:]这意味着在ViewController中的button代码,但我不知道该怎么办了。

Swift 3的两个变化:

select器应该如下所示:

 #selector(ClassName.followButtonClick(_:)) 

该函数应该有一个下划线:

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

请注意,这两个应该在同一个类中,否则,请确保您初始化ClassName类。

如果您希望select器方法( followButtonClick(_:) )在UITableViewCell类中。 删除@IBAction (我不认为你需要它):

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

对于Xcode8的Swift 2.2

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

对于Swift3 ,您需要更改以下内容:

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

附:

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