Swift无法识别的选择器发送到实例错误 [英] Swift unrecognized selector sent to instance error
问题描述
我最近将我的项目从Objective-C转换为Swift,这样每当我点击表格视图单元格中的按钮时,我就会获得此错误。我有多个单元格充满来自mysql服务器的信息。我有两个按钮,一个按钮和一个按钮,当一个单击时,另一个应该显示。我一直在研究这个问题,但我一直坚持这个错误。
I recently converted my project from Objective-C to Swift and in doing so I acquired this error whenever I click a button in the table view's cell. I have multiple cells being filled with information from a mysql server. I have two buttons, a follow button and followed button, when one is clicked the other is supposed to show. I've been working on this for a while but I've been stuck on this error.
当我点击tableview中的按钮时我得到的错误
Error I'm getting when I click the button in the tableview
CustomCellSwift[1425:372289] -[CustomCellSwift.ViewController followButtonClick:]: unrecognized selector sent to instance 0x100b13a40
在CustomCell.swift中
In 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
ViewController.swift
CellForRowAt indexPath
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()
}
}
取消关注按钮代码为t与跟随按钮相同。
Unfollow button code is the same as the follow button.
我认为问题出在按钮选择器(()中的CustomCell.swift中),但错误是 - [CustomCellSwift。 ViewController followButtonClick:]这意味着在ViewController的跟随按钮代码,但我不知道该怎么做。
I think the problem is in CustomCell.swift in the button selector(("")) but the error is saying -[CustomCellSwift.ViewController followButtonClick:] which means in ViewController in the follow button code but I don't know what to do anymore.
推荐答案
两个 Swift 3的更改:
选择器应如下所示:
#selector(ClassName.followButtonClick(_:))
该函数应该有一个下划线:
The function should have an underscore:
@IBAction func followButtonClick(_ sender: UIButton!) { ...
请注意,这两个应该在同一个类中,否则,请确保初始化 ClassName
class。
Notice that these two should be in the same class, otherwise, make sure you initialize the ClassName
class.
如果你想要选择器方法( followButtonClick(_:)
) UITableViewCell
类。删除 @IBAction
(我认为你不需要它):
If you want the selector method(followButtonClick(_:)
) to be in the UITableViewCell
class. Remove @IBAction
(I don't think you need it there):
func followButtonClick(_ sender: UIButton!) { ...
这篇关于Swift无法识别的选择器发送到实例错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!