Ios 如何从集合视图打开viewcontroller?

Ios 如何从集合视图打开viewcontroller?,ios,swift,Ios,Swift,我是ios开发新手,我想知道如何从集合视图打开新的viewcontroller 我有我的收藏视图,现在我想在按下图像1时打开我的viewcontroller颜色紫色,在按下图像2时打开mu viewcontroller颜色青色 我想知道用户单击哪个图像以显示viewcontroller 我添加了一个图像 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

我是ios开发新手,我想知道如何从集合视图打开新的viewcontroller

我有我的收藏视图,现在我想在按下图像1时打开我的viewcontroller颜色紫色,在按下图像2时打开mu viewcontroller颜色青色

我想知道用户单击哪个图像以显示viewcontroller

我添加了一个图像

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  let secondVC = self.storyboard?.instantiateViewController(withIdentifier: "SecondView") as! SecondViewController
  secondVC.modalPresentationStyle = .overCurrentContext
/// if use navigation controller
  self.navigationController?.pushViewController(secondVC, animated: true)
/// if use viewController
self.present(secondVC, animated: true, completion: nil)

您可以使用UICollectionViewDelegate的didSelectItemAt,或者在每个单元格上添加一个按钮,并实现操作事件。希望这能对您有所帮助。您可以使用UICollectionViewDelegate的didSelectItemAt或在每个单元格上添加一个按钮并实现操作事件。希望这能帮助你。