访问 UICollectionView 的父 UIViewController [英] Access a UICollectionView's parent UIViewController
问题描述
我的问题很简单.我有一个包含 UICollectionView 的 UIViewController.在初始化我的单元格时,我为每个单元格添加了一个手势识别器,这样当你点击并按住它时,它会调用一个带有选择器的函数.
My question is fairly straightforward. I have a UIViewController containing a UICollectionView. In initialising my cells, I add a gesture recogniser to each of them so that when you tap and hold it calls a function with a selector.
这个函数然后创建一个我想展示的 UIAlertController.(基本上,你拿着一个单元格,它会问你是否要删除它,如果你说是,它会从 CollectionView 中删除它)
This function then creates a UIAlertController which I want to present. (Basically, you hold a cell, it asks you if you want to delete it and if you say yes it eliminates it from the CollectionView)
问题是我无法从 UICollectionView 显示 UIAlertController,因为它不是 ViewController.
The problem is that I can't present a UIAlertController from my UICollectionView because it isn't a ViewController.
我想以编程方式获取包含 UICollectionView 的 UIViewController 以从 UICollectionView 实现中的函数显示此警报.
I want to programmatically get the UIViewController that contains the UICollectionView to present this alert from a function that is inside the UICollectionView implementation.
推荐答案
我通过在我的自定义 UICollectionViewCell
中创建一个协议并将这些事件委托回 UIViewController
来做到这一点,像这样
I do this by making a protocol in my custom UICollectionViewCell
and delegate these events back to the UIViewController
, something like this
在您的 MyCollectionViewCell
protocol MyCollectionViewCellDelegate: class {
func didLongPressCell()
}
class MyCollectionViewCell:UICollectionViewCell {
weak var delegate:MyCollectionViewCellDelegate?
func longPressAction() {
if let del = self.delegate {
del.didLongPressCell
}
}
}
然后回到你的 MyViewController
class MyViewController:UIViewController, MyCollectionViewCellDelegate {
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! MyCollectionViewCell
cell.delegate = self
return cell
}
func didLongPressCell() {
// do what you want with the event from the cell here
}
}
要记住的重要部分是为每个单元格设置委托
The important bits to remember are to set the delegate for each cell
cell.delegate = self
并在要接收事件的视图控制器中采用新协议
And adopt your new protocol in the view controller that you want to receive the events in
class MyViewController:UIViewController, MyCollectionViewCellDelegate
我还没有测试过这段代码,我不确定在像这样的每个单元格中存储对 viewController 的引用的最佳实践,但我做了一些非常相似的事情,让我知道你是怎么做的.
如果您已经将 UICollectionView
子类化,则将其传递给视图控制器的引用,以便您可以像这样使用它.
if you have subclassed your UICollectionView
then pass it a reference to the view controller so that you can use it like so.
你的 MyViewController
现在看起来像这样
Your MyViewController
would now look like this
class MyViewController:UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let collectionView = MyCollectionView()
collectionView.viewController = self
self.view.addSubview(collectionView)
}
}
以及您的自定义集合视图 MyCollectionView
class MyCollectionView:UICollectionView, MyCollectionViewCellDelegate {
weak var viewController:UIViewController?
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! MyCollectionViewCell
cell.delegate = self
return cell
}
func didLongPressCell() {
if let vc = self.viewController {
// make use of the reference to the view controller here
}
}
}
UICollectionViewCell
和之前一样
The UICollectionViewCell
would be the same as before
这篇关于访问 UICollectionView 的父 UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!