Swift: 在 UICollectionViewCell 上推送视图控制器

Swift: Pushing a View controller on a UICollectionViewCell

我有一个集合视图,每个单元格都有一个点击手势,点击时我想启动另一个 ViewController,就像我在使用导航栏时所做的那样(ViewController 有一个 X应该弹出它的按钮)。 为此,我通常使用 self.navigationController?.present,但在这种情况下,因为它是一个 UICollectionViewCell,所以我不能使用 present 函数。 我试过了,但没用:

    func handleTap(){
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "driver")
        self.window?.rootViewController?.presentedViewController?.addChildViewController(controller)
    }

出于某种原因,可选项中全部为 nil。 这甚至是一种合乎逻辑的方法吗?什么是解决这个问题的好方法?

使用此方法实现推送转接。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    // handle tap events
    print("You selected cell #\(indexPath.item)!")

}

您也可以使用故事板来执行动作