【问题标题】:Swift: Pushing a View controller on a UICollectionViewCellSwift:在 UICollectionViewCell 上推送视图控制器
【发布时间】:2017-02-06 10:08:39
【问题描述】:

我有一个集合视图,每个单元格都有一个点击手势,当被点击时我想启动另一个 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)
    }

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

【问题讨论】:

  • 为什么不用didSelectItemAtindexPath的didSelectItemAtindexPath方法。
  • 这个我不知道,去看看!
  • @NiravD 的建议是正确的,也是最好的方法。使用此方法,然后您可以通过简单的 self.present... 来展示您的 VC。
  • @Mannopson 你不能从UICollectionViewCell 调用self.present。
  • @NiravD 我认为他的意思是我应该从 UICollectionView 类调用 self.present

标签: swift uicollectionviewcell viewcontroller


【解决方案1】:

使用此方法实现push segue。

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

}

您也可以使用情节提要来执行操作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    • 2020-05-19
    • 1970-01-01
    • 2016-10-22
    • 2010-12-11
    相关资源
    最近更新 更多