BroSimple BroSimple - 16 days ago 6
iOS Question

Swift unrecognized selector sent to instance error

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.

Error I'm getting when I click the button in the tableview

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


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

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
}


Follow Button Code

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

}
}


Unfollow button code is the same as the follow button.

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.

Answer

Two changes for Swift 3:

The selector should look like:

#selector(ClassName.followButtonClick(_:))

The function should have an underscore:

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

Notice that these two should be in the same class, otherwise, make sure you initialize the ClassName class.

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!) { ...