gpt4 book ai didi

ios - Swift 无法识别的选择器发送到实例错误

转载 作者:搜寻专家 更新时间:2023-11-01 06:17:42 25 4
gpt4 key购买 nike

我最近将我的项目从 Objective-C 转换为 Swift,在这样做的过程中,每当我单击表格 View 单元格中的按钮时,我都会收到此错误。我有多个单元格被来自 mysql 服务器的信息填充。我有两个按钮,一个关注按钮和一个关注按钮,当点击一个时,另一个应该显示。我已经为此工作了一段时间,但一直卡在这个错误上。

当我点击表格 View 中的按钮时出现错误

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
}

跟随按钮代码

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

}
}

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

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

最佳答案

Swift 3 的两个变化:

选择器应该如下所示:

#selector(ClassName.followButtonClick(_:))

函数应该有一个下划线:

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

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

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

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

关于ios - Swift 无法识别的选择器发送到实例错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40756801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com