您遇到的错误是因为 UICollectionReusableView 没有 storyboard 属性。它也没有navigationController 属性。
您需要在包含它的 viewController 中而不是在可重用视图中执行该转换。为了做到这一点,你需要一个自定义委托或一个回调块来告诉视图控制器标题被点击并触发推送。
使用块/闭包/回调:
在可重用视图中添加你的完成闭包:
var completion: (() -> Void)?
接下来,在您的 goToMovie func 中删除这两行并添加,然后像这样调用闭包:
completion?()
然后在您声明标题的视图控制器中触发转换,如下所示:
. . .
headerView.completion = {
let storyboard = UIStoryboard(name: "WhateverYourSBNameIs", bundle: nil)
let aaaVC = storyboard.instantiateViewController(withIdentifier: "WhateverYourIdentifierIs") as! AAAViewController
self.navigationController?.pushViewController(st, animated: true)
}
使用自定义委托
创建协议:
protocol HeaderDelegate {
func headerTapped(sender: UICollectionReusableView)
}
然后在头部添加委托属性
var delegate: HeaderDelegate?
那么当你声明头部时:
. . .
header.delegate = self
. . .
然后让视图控制器符合委托:
extension ViewController: HeaderDelegate {
func headerTapped(sender: UITableViewCell) {
let storyboard = UIStoryboard(name: "WhateverYourSBNameIs", bundle: nil)
let aaaVC = storyboard.instantiateViewController(withIdentifier: "WhateverYourIdentifierIs") as! AAAViewController
self.navigationController?.pushViewController(st, animated: true)
}
}
如果视图控制器不需要知道哪个标头被点击,您可以从委托函数中删除 sender: UITableViewCell 参数。
希望这会有所帮助。我个人会使用关闭 FWIW。