[英]How to navigate from an embedded collection view’s cell to another view controller without using Storyboard or IB?
About the app (UICollectionView within UICollectionView): 关于应用程序(UICollectionView中的UICollectionView):
HomeViewController
as root view controller HomeViewController
作为根视图控制器 HomeViewController
contains CategoryCollectionView
HomeViewController
包含CategoryCollectionView
CategoryCollectionView
is present one ItemCollectionView
CategoryCollectionView
每个单元中都存在一个ItemCollectionView
ItemCollectionView
contains many ItemCell
ItemCollectionView
包含许多ItemCell
Objective : When user clicks on an ItemCell
, the app should navigate to a different view controller, the ItemViewController. 目标 :当用户单击
ItemCell
,应用程序应导航到另一个视图控制器ItemViewController。 I'm developing this app completely using code. 我正在完全使用代码开发此应用程序。 So I want to do this without Storyboard segues or IB.
所以我想在没有Storyboard segue或IB的情况下执行此操作。 I haven't been able to figure it out so far.
到目前为止,我还无法弄清楚。 If you could point me in the right direction, it would be great.
如果您能指出正确的方向,那就太好了。 Thanks.
谢谢。
I tried the following, but they didn't work: 我尝试了以下方法,但是它们没有起作用:
First method : Accessing the window's root view controller from within the CategoryCollectionView
's cell 第一种方法 :从
CategoryCollectionView
的单元格中访问窗口的根视图控制器
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let currentNavCon = self.window?.rootViewController!.navigationController
currentNavCon?.pushViewController(ItemViewController(), animated: true)
}
Second method : I defined a function itemCellClicked
in HomeViewController
, and called it from within didSelectItemAt
of CategoryCollectionView
's cell. 第二种方法 :我在
HomeViewController
定义了一个函数itemCellClicked
,并从CategoryCollectionView
的单元格的didSelectItemAt
中调用了它。
func itemCellClicked(_ sender: CatalogViewCategoryCell, _ position: Int) {
let itemViewController = ItemViewController()
navigationController?.pushViewController(itemViewController, animated: true)
}
And, inside the cell: 并且,在单元格内:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
HomeViewController().itemCellClicked(self, indexPath.item)
}
Pushing view controller from cell is never a good idea. 从单元格推送视图控制器从来都不是一个好主意。 You need to propagate your click event from
ItemCell
to all the way to HomeViewControlller
. 您需要将Click事件从
ItemCell
传播到HomeViewControlller
。
In your CategoryCollectionView
define a public property of type closure. 在您的
CategoryCollectionView
定义一个闭包类型的公共属性。
var onDidSelectItem: ((IndexPath) -> ())?
Now in CategoryCollectionView
's didSelectItem
, call the closure like this. 现在在
CategoryCollectionView
的didSelectItem
,像这样调用闭包。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.onDidSelectItem?(indexPath)
}
In your HomeViewControlller
's cellForRow
, receive the callback. 在您的
HomeViewControlller
的cellForRow
,接收回调。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
categoryCollectionView.onDidSelectItem = {(indexPath) in
//item at indexPath clicked, do whatever you want to do with it.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.