我正在使用外部委托文件来处理所有的UICollectionView的处理,我努力让集合视图单元格通过委托文件执行基于选定单元格的segue。Swift UICollectionView委托执行segue
这就是我目前拥有的委托文件中:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
let mainController = MainController()
mainController.performSegue(withIdentifier: "detailSegue", sender: cell)
}
,并在主控制器我有:
override func performSegue(withIdentifier identifier: String, sender: Any?) {
if identifier == "detailSegue" {
let detailController = DetailController()
self.present(detailController, animated: true)
}
}
我得到的错误:
Warning: Attempt to present <DetailController: 0x7fbed8e6e4b0> on <MainController: 0x7fbedda79830> whose view is not in the window hierarchy!
我想我可以通过委托调用参考,它会呈现控制器。
感谢
工作,非常感谢。 – user7684436
委托人应该弱,不要导致保留周期....'弱var viewController:UIViewController?'也最好通过使用协议解耦代码 – Luzo