У меня есть один пользовательский UICollectionReusableView
заголовок UICollectionView
. Внутри этого представления у меня есть две кнопки. Я создал метод делегата для выполнения действий с кнопками, однако они не вызываются.
Я добавляю свой заголовок в UICollectionViewDataSource
следующим образом;
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: Identifier.exploreCollectionReusableView, for: indexPath) as! ExploreCollectionReusableView
return headerView
}
Я настраиваю свой протоколметоды и добавить переменную делегата внутри пользовательского заголовка;
protocol SelectedDidSeeAll {
func seeAllCategories()
func seeAllItems()
}
var delegate: SelectedDidSeeAll?
Я добавляю цели к кнопкам;
seeAllCategoriesButton.addTarget(self, action: #selector(seeAllCategories), for: .touchUpInside)
seeAllItemsButton.addTarget(self, action: #selector(seeAllItems), for: .touchUpInside)
Я устанавливаю делегата на себя в UIViewController
viewDidLoad()
exploreCollectionReusableView.delegate = self
Я реализую функции @objc
;
@objc func seeAllCategories() {
delegate?.seeAllCategories()
}
@objc func seeAllItems() {
delegate?.seeAllItems()
}
Наконец, я соответствую методу делегата в UIViewController
;
extension ExploreViewController: SelectedDidSeeAll {
func seeAllCategories() {
// Do Something
print("something")
}
func seeAllItems() {
// Do Something
print("something")
}
}
Журнал консоли делегата
При печати полученного делегата;
Optional(<bidr.ExploreViewController: 0x7fd466e5d0b0>)
Итак, я предполагаю, что сделал все необходимое для реализации метода делегата, однако нажатие кнопок не имеет никакого эффекта.