我正在使用外部委託文件來處理所有的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