我正在尝试重构我的代码,当点击按钮时,我似乎无法从 SearchController 激活 handleFavoriteStar() 操作。我正在关注 LBTA 的重构视频:https ://youtu.be/F3snOdQ5Qyo
公式单元格:
class FormulasCell: UITableViewCell {
var searchController: SearchController! {
didSet {
buttonStar.addTarget(searchController, action: #selector(searchController.handleFavoritedStar), for: .touchUpInside)
}
}
var buttonStar: UIButton = {
let button = UIButton()
button.setImage( #imageLiteral(resourceName: "GrayStar") , for: .normal)
button.tintColor = UIColor.greyFormula
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
}
搜索控制器:
class SearchController: UIViewController, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let formulaCell = FormulasCell()
formulaCell.searchController = self
setupTableView()
}
@objc func handleFavoritedStar() {
print("Added to Favorites")
}
}