首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UICollectionReusableView中未调用委托

UICollectionReusableView中未调用委托
EN

Stack Overflow用户
提问于 2019-11-11 17:45:56
回答 1查看 248关注 0票数 0

我有一个自定义的UICollectionReusableView作为UICollectionView标头。在这个视图中,我有两个按钮。我已经创建了一个委托方法来执行按钮操作,但是它们没有被调用。

我在UICollectionViewDataSource中添加我的头,如下所示;

代码语言:javascript
复制
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
}

我设置了我的协议方法,并在自定义头部中添加了一个委托变量;

代码语言:javascript
复制
protocol SelectedDidSeeAll {
    func seeAllCategories()
    func seeAllItems()
}

var delegate: SelectedDidSeeAll?

我将目标添加到按钮中;

代码语言:javascript
复制
seeAllCategoriesButton.addTarget(self, action: #selector(seeAllCategories), for: .touchUpInside)
seeAllItemsButton.addTarget(self, action: #selector(seeAllItems), for: .touchUpInside)

我在UIViewController viewDidLoad()中将委托设置为self

代码语言:javascript
复制
exploreCollectionReusableView.delegate = self

我实现了@objc函数;

代码语言:javascript
复制
@objc func seeAllCategories() {
    delegate?.seeAllCategories()
}

@objc func seeAllItems() {
    delegate?.seeAllItems()
}

最后,我遵循UIViewController中的委托方法;

代码语言:javascript
复制
extension ExploreViewController: SelectedDidSeeAll {

func seeAllCategories() {
    // Do Something
    print("something")
    }

func seeAllItems() {
    // Do Something
    print("something")
    }
}

委派控制台日志

当打印委托时,我得到了;

代码语言:javascript
复制
Optional(<bidr.ExploreViewController: 0x7fd466e5d0b0>)

因此,我假设我已经完成了实现委托方法所需的所有操作,但是,单击按钮没有任何效果。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-11 17:51:36

collectionView(_: viewForSupplementaryElementOfKind: at:)中设置delegate,而不是viewDidLoad(),即

代码语言:javascript
复制
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: Identifier.exploreCollectionReusableView, for: indexPath) as! ExploreCollectionReusableView
    headerView.delegate = self
    return headerView
}

viewDidLoad()中,可能未设置exploreCollectionReusableView

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58798964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档