【发布时间】:2020-02-03 08:58:44
【问题描述】:
我在 HomeViewController 上显示的单独 swift 文件中设置了 UICollectionView。我想在选择集合视图单元格时推送一个 DetailViewController。我想以编程方式做到这一点,没有故事板。我一直使用“navigationController.pushViewController(vc, animated: true)”推送 ViewController,但我无法从 UICollectionView 的“didSelectItemAt”函数中访问 navigationController。如果重要的话,我确实设置了一个自定义 UICollectionViewCell 并用于单元格。
我怎样才能做到这一点? collectionView 显示正确,当我选择一个单元格时,我知道 didSelectItemAt 正在运行,因为我的打印语句“Did select item at ...”显示在控制台中。
经过数小时的谷歌搜索,我无法弄清楚。我仍在学习 M-V-C 结构,所以我可能在一切设置方面存在根本缺陷。我还看到我可能需要创建一个自定义委托来处理这个问题,但我觉得我缺少一些更简单的东西,因为 didSelectItemAt 函数就在那里。
这是我的 UICollectionView swift 文件中的相关代码:
class HomeDataController: NSObject, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("Did select item at...")
// this is where I am trying to push the DetailViewController
}
}
这是我的 HomeViewController:
class HomeViewController: UIViewController {
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.minimumLineSpacing = 30
layout.sectionInset.top = 20
layout.sectionInset.bottom = 20
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.translatesAutoresizingMaskIntoConstraints = false
cv.backgroundColor = UIColor(named: "background")
cv.register(HomeCustomCell.self, forCellWithReuseIdentifier: "homeCell")
return cv
}()
let dataController: HomeDataController = {
let dataController = HomeDataController()
return dataController
}()
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self.dataController
collectionView.delegate = self.dataController
view.addSubview(collectionView)
}
}
【问题讨论】:
-
您需要将对 navigationController 的引用传递到您打算从中推送的对象中。
-
谢谢!我不知道如何完成你所说的,所以我最终尝试了另一个评论者答案中的代表。
标签: ios swift view uiviewcontroller uicollectionview