首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >单击集合视图的单元格时打开另一个视图控制器

单击集合视图的单元格时打开另一个视图控制器
EN

Stack Overflow用户
提问于 2021-09-11 03:23:34
回答 2查看 300关注 0票数 0

我是swift的新手,我跟随这个视频做了一个集合视图,它工作得很好。但是在从一个单元格到另一个单元格单击时,单击并不起作用。

https://www.youtube.com/watch?v=TQOhsyWUhwg

代码语言:javascript
代码运行次数:0
运行
复制
func collectionView(_ collectionView: UICollectionView, 
  didSelectItemAt indexPath: IndexPath) {
    print("Cell \(indexPath.row + 1) clicked")
  }

在这里,它打印选定的单元格。当单元格被单击时,我只需要打开另一个视图。有人能帮我吗。

EN

回答 2

Stack Overflow用户

发布于 2021-09-11 04:55:03

你只需要创建另一个视图控制器的对象并推送它。像这样:

代码语言:javascript
代码运行次数:0
运行
复制
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "your_view_identifier") as! Your_ViewController
self.navigationController?.pushViewController(nextViewController, animated: true)
票数 0
EN

Stack Overflow用户

发布于 2021-09-11 06:25:31

让我们猜猜您想要导航的viewController是SecondViewController。故事板的名字是Main

导航ViewController的步骤

您需要创建该ViewController

  • You的实例,以便从导航控制器

推送该viewController

代码语言:javascript
代码运行次数:0
运行
复制
class SecondViewController: UIViewController {
    
}

var secondViewController: SecondViewController {
    let st = UIStoryboard(name: "Main", bundle: nil)
    let vc = st.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
    return vc
}

现在,如果你想从单元格点击导航视图控制器。只需将视图控制器推送到导航堆栈中。

代码语言:javascript
代码运行次数:0
运行
复制
func collectionView(_ collectionView: UICollectionView, 
  didSelectItemAt indexPath: IndexPath) {
    print("Cell \(indexPath.row + 1) clicked")
    self.navigationController?.pushViewController(secondViewController, animated: true)
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69139761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档