将数据从 Header 检索到 parent 的视图

Retrieving data from Header to the parent's view

我有一个自定义 UICollectionViewController,其中包含自定义 header 和单元格。

我已经实现了一个协议,collectionView 响应该协议,所以通过这种方式,我在 collectionView 中知道何时在 [=47= 中访问了 object(例如按下按钮) ] class,到目前为止还不错。

我想做的是,当我在 collectionView 中点击一个按钮时,我需要 header 来检索它拥有的一些数据到 collectionView

假设我点击 collectionView 中的保存按钮,为了执行保存,我需要 header.

中的当前数据

我怎样才能做到这一点?

这是一些当前代码:

    import UIKit

// collection view class
class EditUserProfileHeader: UICollectionViewCell, UITextViewDelegate {
    var delegate: CustomHeaderDelegate?

    //(... some default code ...)
    override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerId, for: indexPath) as! CustomHeader
        header.delegate = self
        return header
    }

    // header delegate conform
    func didTapPhoto() {
        // this fires everytime the button is pressed inside the header
        print("didTapPhoto was tapped inside the header")
    }


    func didTapSave() {
        //here I need the header's data from it's labels and so on
    }

}


protocol CustomHeaderDelegate {
    func didTapPhoto()
}



class CustomHeader: UICollectionViewCell { 
    var delegate: CustomHeaderDelegate?
    //(... some class regular code ...)

    func didTapPhoto() {
        delegate?.didTapPhoto() // here I pass to the collectionView that this button was pressed in the header
    }

}

如您所见,每当 header 中出现 didTapButton 时,我都会调用委托函数,因此我知道 collectionView 中按钮被触发。

现在我需要尝试相反的方法;当我点击 collectionView 中的一个按钮时,我需要 header 知道它并且它检索所需的数据。

任何人都可以帮助我吗?

我试图实现另一个协议来做相反的事情,但我可能做错了,因为它不起作用,主要是因为我不能 'say' 在 header class parent 的代表符合那个特定的代表,当我实例化 header object 并设置它的 delegate to self[ 时,我在 collectionView 中做了吗? =22=]

谢谢

我设法访问了所需的 header 在 didTapSave() 函数中插入以下行

let header = collectionView?.supplementaryView(forElementKind: UICollectionElementKindSectionHeader, at: IndexPath(item: 0, section: 0)) as? CustomHeader

因为我可以访问 header 更新的数据。