简体   繁体   English

如何在不使用情节提要或IB的情况下从嵌入式集合视图的单元导航到另一个视图控制器?

[英]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):

  • TabBarController is the app window's root view controller TabBarController是应用程序窗口的根视图控制器
  • TabBarController contains 3 Navigation Controllers. TabBarController包含3个导航控制器。
  • 1st Navigation Controller has HomeViewController as root view controller 第一个导航控制器具有HomeViewController作为根视图控制器
  • HomeViewController contains CategoryCollectionView HomeViewController包含CategoryCollectionView
  • Inside each cell of CategoryCollectionView is present one ItemCollectionView CategoryCollectionView每个单元中都存在一个ItemCollectionView
  • Each 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. 现在在CategoryCollectionViewdidSelectItem ,像这样调用闭包。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.onDidSelectItem?(indexPath)
}

In your HomeViewControlller 's cellForRow , receive the callback. 在您的HomeViewControlllercellForRow ,接收回调。

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.

相关问题 如何将数据从集合视图单元传递到新的视图控制器(以编程方式且没有情节提要)? - How can I passed data from collection view cell to new view controller (programmatically and without storyboard)? 如何优雅地从一个嵌入式视图导航到另一个视图(在IB中使用布局)? - How can I elegantly navigate from one embedded view to another (with layout in IB)? 如何将集合视图单元格的indexPath传递给另一个视图控制器 - How to pass a collection view cell's indexPath to another view controller 当我轻按任何集合视图单元格时(不点击最后一个单元格),如何移动另一个视图控制器? - How to move another view controller when i tapped any collection view cell (without taping last cell)? 如何使用情节提要中定义的单元格来计算集合视图单元格的高度? - How to calculate collection view cell height using cell defined in storyboard? 如何使用SlideMenuViewcontroller从Mainviewcontroller导航到另一个视图控制器 - How to navigate from Mainviewcontroller to another view controller using SlideMenuViewcontroller 如何使用 Swift 从一个视图控制器导航到另一个视图控制器 - How to Navigate from one View Controller to another using Swift 如何从一个视图控制器导航到另一视图控制器? - How to navigate from one view controller to another view controller? 如何从popoverpresentation视图控制器导航到另一个视图控制器 - How to navigate from popoverpresentation view controller to another view controller Swift 4:将数据从集合视图单元格传递到另一个没有故事板的视图控制器 - Swift 4: Passing data from a collection view cell into another view controller without storyboards
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM