访问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
In your MyCollectionViewCell
protocol MyCollectionViewCellDelegate: class {
func didLongPressCell()
}
class MyCollectionViewCell:UICollectionViewCell {
weak var delegate:MyCollectionViewCellDelegate?
func longPressAction() {
if let del = self.delegate {
del.didLongPressCell
}
}
}
然后返回 MyViewController
Then back in your 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
子类化,则将其传递给视图控制器,以便您可以像s一样使用它o。
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
And your custom collection view 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屋!