I am trying to call an addTarget from both of my two buttons in a reasuable cell for a tableview. I would like to pass both buttons into the #selector function so that I can change the isSelected property for one or both buttons at the same time (with the same addTarget) as a user clicks either button (both buttons should not be selected at once, so if the sender is being changed to be isSelected = true I want a line to make sure the other button is marked as isSelected = false, and there are two of these calls - one for each button as sender).
What is the easiest way to do this? I am having trouble passing both buttons in as arguments into the #selector call to my objc function. I keep getting the error "Argument of '#selector' does not refer to an '@objc' method, property, or initializer" even though it definitely is defined in the ViewController class (not sure if it makes a difference but the addTarget line is coming from an extension off of the ViewController class rather than from directly within the class).
I've tried Xcode's recommendation to change the selector call to be #selector(ViewController.clearVotingButtonTapped(sender:otherButton:)). This is in line with how other posts have said to call an objc function with multiple parameters, but I have a few problems with this:
- The app crashes when I run it
- There are just the names of the arguments rather than the names of actual parameters I am looking to pass in and manipulate from within the objc function itself (the names of the variables that I would like to pass in are clearButton which is set equal to cell.clearVoteButton and spotCheckButton which is set equal to cell.spotCheckVoteButton).
I'm sure there's something about the syntax of passing in arguments/objects that I am missing, but I'm having trouble figuring it out, what's the move team? Below I show the objc functions as written in the ViewController class and then the call to these functions from the tableView function out of my extensions of the ViewController class (the extension houses all of my tableview functions/settings). Thanks!
//behavoir for when either voting button is tapped
@objc func votingButtonTapped(sender: UIButton, otherButton: UIButton) {
if sender.isSelected == true {
sender.isSelected = false
}
else {
sender.isSelected = true
otherButton.isSelected = false
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? CustomTableViewCell else {fatalError("Unable to create cell")}
let stop = currentStopList[indexPath.row]
//glossing over irrelevant code
let clearButton = cell.clearVoteButton
let spotCheckButton = cell.spotCheckVoteButton
clearButton.setTitleColor(.white, for: .normal)
clearButton.setTitle(stopClearVotesStr, for: .normal)
//glossing over irrelevant code
clearButton.setBackgroundImage(clearVoteImageUnselected, for: .normal)
clearButton.setBackgroundImage(clearVoteImageSelected, for: .selected)
clearButton.addTarget(self, action: #selector(votingButtonTapped(sender: clearButton, otherButton: spotCheckButton)), for: .touchUpInside) //triggers incrementing votes and changing background image
spotCheckButton.setTitleColor(.white, for: .normal)
spotCheckButton.setTitle(stopSpotCheckVotesStr, for: .normal)
//glossing over irrelevant code
spotCheckButton.setBackgroundImage(spotCheckVoteImageUnselected, for: .normal)
spotCheckButton.setBackgroundImage(spotCheckVoteImageSelected, for: .selected)
spotCheckButton.addTarget(self, action: #selector(votingButtonTapped(sender: spotCheckButton, otherButton: clearButton)), for: .touchUpInside) //triggers incrementing votes and changing background image
return cell
}