好的,我仍然是新的协议,但我需要从另一个类调用一个函数(我知道在头类中调用时工作)。
要做到这一点,在Im调用函数的类(它是一个MSMessagesAppViewController)我有:
public protocol MSMessagesAppViewControllerDelegate: class {
func shrinkHeight(height:CGFloat)
func growHeight(height: CGFloat)
}
weak var delegate: MSMessagesAppViewControllerDelegate?
delegate?.shrinkHeight(height: 90)
然后我在class HeaderCollectionReusableView: UICollectionReusableView, MSMessagesAppViewControllerDelegate
中有实际的功能:
func shrinkHeight(height:CGFloat)
{
print("WORKING!!"); //NOT printed
UIView.animate(withDuration: 0.7, delay: 0.0, usingSpringWithDamping:
0.6, initialSpringVelocity: 1.1, options: [], animations: {
//thing being animated
self.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: self.screenSize.height * (height/self.screenSize.height))
//change out
self.squareLogo.isHidden = true
self.logoLabel.isHidden = false
}, completion: { finished in
//code that runs after the transition is complete here
})
}
func growHeight(height:CGFloat)
{
它编译但我知道上面的函数没有被调用,因为没有print语句。我在这里做错了什么?
答案 0 :(得分:0)
几秒钟前我遇到了同样的问题。我觉得你和我有同样的问题。
在您实现MSMessagesAppViewControllerDelegate的自定义UICollectionView类中,您必须为可重用单元添加委托。
类似的东西:
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if kind == UICollectionElementKindSectionHeader {
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerCell", for: indexPath) as! YourCustomClassForHeader
headerView.delegate = self // here was my problem, I just forgot to set the delegate
return headerView
} else {
return UICollectionReusableView()
}
}