【问题标题】:Swift unrecognized selector sent to instance errorSwift无法识别的选择器发送到实例错误
【发布时间】:2016-11-23 05:34:50
【问题描述】:

我最近将我的项目从 Objective-C 转换为 Swift,这样做时,每当我单击表格视图单元格中的按钮时,都会出现此错误。我有多个单元格填充了来自 mysql 服务器的信息。我有两个按钮,一个跟随按钮和一个跟随按钮,当单击一个时,另一个应该显示。我已经为此工作了一段时间,但一直被这个错误困扰。

单击表格视图中的按钮时出现错误

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 索引路径

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
}

关注按钮代码

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

    }
}

取消关注按钮代码与关注按钮相同。

我认为问题出在按钮 selector(("")) 中的 CustomCell.swift 中,但错误是 -[CustomCellSwift.ViewController followButtonClick:],这意味着在 ViewController 中的后续按钮代码中,但我不知道该怎么办了。

【问题讨论】:

  • 转换为Swift2.2Swift3?
  • 我已转换为 Swift 3 先生
  • 查看Swift3的答案

标签: ios swift uitableview selector


【解决方案1】:

Swift 3 的两个变化:

选择器应如下所示:

#selector(ClassName.followButtonClick(_:))

函数应该有一个下划线:

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

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

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

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

【讨论】:

  • 你是一个美丽的人谢谢你,这一切都是因为(_发件人:UIButton!)中的下划线
  • 顺便说一句,我有另一个类似的问题,你可以发布相同的答案,我会给你,因为它会修复错误
  • 在我的情况下,这是因为我忘记更改自定义类 (Main.storyboard) 中的模块,因为我将一个项目重用于新项目
【解决方案2】:

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

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

与:

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

【讨论】:

  • 好的,所以我尝试了这个并说它无法使用 self 在 CustomCell.swift 中找到 followButtonClick,所以我尝试了 ViewController.followedButtonClick,它也找不到它或者没有确切的成员
【解决方案3】:

对于Swift 2.2Xcode 8

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

【讨论】:

  • 使用 Swift 3 先生
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多