カスタムヘッダーとセルを持つカスタムUICollectionViewController
があります。ヘッダーから親ビューにデータを取得する
私は、このようにして、collectionViewが応答するプロトコルを実装しました。このように、ヘッダークラスのオブジェクトがアクセスされたときに、collectionViewで知っています。私はcollectionView
で、ボタンをタップすると、私はcollectionView
にそれが所有していることをいくつかのデータを取得するために、ヘッダが必要であることを私はそれをやりたい
は、私がcollectionView
に保存ボタンをタップすると言うことができます保存を実行するには、ヘッダから現在のデータが必要です。
どうすればこの問題を解決できますか?
はここで、現在のコードの一部です:
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
}
}
あなたが見ることができるように、私は、私は私がcollectionView
に知っているので、デリゲート関数を呼び出すdidTapButton
がヘッダーで発生するたびに、デリゲートを実装しましたことボタンが鳴った。
今私は反対にする必要があります。私がcollectionView
の中のボタンをタップすると、それを知るためにヘッダが必要であり、それが所望のデータを取得する必要があります。
誰も私にこの手を差し伸べることはできますか?
逆のことをする別のプロトコルを実装しようとしましたが、うまくいきませんでした。なぜなら、ヘッダークラスで親のデリゲートがその特定のデリゲートに準拠していると言うことができず、 Iヘッダーオブジェクトをインスタンス化し、そのdelegate to self
おかげで設定した場合、私は
解決済みのように質問してください/解消しました。 –