【发布时间】:2017-05-29 23:57:21
【问题描述】:
我有一个自定义的UICollectionViewController,带有自定义的标题和单元格。
我已经实现了一个协议,collectionView 会在其中做出响应,所以通过这种方式,我知道在 collectionView 中何时访问了 header 类中的对象(例如按下按钮),到目前为止一切顺利。
我想要做的是,当我在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
}
}
如您所见,我已经实现了一个委托,只要 didTapButton 出现在标题中,我就会调用委托函数,因此我在 collectionView 中知道该按钮已触发。
现在我需要尝试相反的方法;当我点击collectionView 内的按钮时,我需要标题知道它并检索所需的数据。
谁能帮我解决这个问题?
我试图实现另一个协议来做相反的事情,但我可能做错了,因为它不起作用,主要是因为我不能在头类中“说”父的委托符合那个特定的委托,有当我实例化标头对象并设置其delegate to self 时,我在collectionView 中做到了
谢谢
【问题讨论】:
-
将问题标记为已解决/已解决。
标签: ios swift3 delegates swift-protocols