I'm trying to make a button do something in another view controller. I made a delegate, added a function with a requirement of having an indexPath so I could use it to delete items in an array, but when I make a variable indexPath equal to IndexPath.self and try feeding it into the actual IBAction, it spits out the error shown in the title. Here is my code for my first view controller, where the IBAction and protocol is defined.
import UIKit
protocol ToDoItemCellDelegate: AnyObject {
func didTapX(with indexPath: IndexPath)
}
class ToDoItemCell: UITableViewCell {
weak var delegate: ToDoItemCellDelegate?
static let identifier = "ToDoItemCell"
static func nib() -> UINib {
return UINib(nibName: "ToDoItemCell", bundle: nil)
}
@IBOutlet var xButton: UIButton!
private var indexPath = IndexPath.self
@IBAction func didTapX(_ sender: UIButton) {
delegate?.didTapX(with: indexPath)
}
override func awakeFromNib() {
super.awakeFromNib()
}
}
CodePudding user response:
You have mistaken type and entity. Your delegate expects an entity and your var only provides a type. You stated in your comment that you do not need to provide any meaningfull value here so you have multiple possibilities:
Remove the argument from the delegate:
protocol ToDoItemCellDelegate: AnyObject { func didTapX() }
or create a default value while calling the delegate and remove the property:
@IBAction func didTapX(_ sender: UIButton) { delegate?.didTapX(with: IndexPath(item: 0, section: 0)) }
or assign a defalut to the property and use it while calling the delegate:
private var indexPath = IndexPath(item: 0, section: 0)