我有一个显示图像缩略图的UICollectionView
。当我点击这个单元格(缩略图)时,我使用下面的功能来推动另一个UIViewController
,然后启用be下载并查看缩略图的放大图像。每个细胞内都有一个UIButton
。我想介绍另一个viewController
将作为一个自定义弹出窗口,我希望显示图像的更多细节,如文件名,日期等。。。当将action
连接设置为自定义UICollectionViewCell
类时,在IBAction
函数方法中,我无法“呈现”此自定义UIViewController
。Xcode
只是不重设“present”。有人能给我建议吗?
class CollectionViewFolder: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate ,UICollectionViewDelegateFlowLayout{
...
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
}
...
}
自定义:
class CollectionViewFolderCell: UICollectionViewCell {
@IBAction func moreInfoBtn(_ sender: Any) {
// DOES NOT RECOGNIZE 'present' - TO PRESENT ANOTHER UIVIEWCONTROLLER
}
}
最佳答案
在CollectionViewFolderCell.swift
中创建协议
protocol CollectionViewFolderCellDelegate {
func collectionViewFolderCellDidPressButton()
}
在
CollectionViewFolderCell
内部声明一个委托如下:var delegate: CollectionViewFolderCellDelegate?
在按钮操作中添加:
@IBAction func moreInfoBtn(_ sender: Any) {
delegate?.collectionViewFolderCellDidPressButton()
}
在cellForItemAtIndexPath方法中添加
cell.delegate = self
func collectionView(collectionView: UICollectionView,
cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewFolderCell
cell.delegate = self
return cell
}
同样,在视图控制器中,您需要符合
CollectionViewFolderCellDelegate
extension CollectionViewFolder: CollectionViewFolderCellDelegate {
// here you can present your desired view controller
}
关于ios - 如何在UICollectionView之上呈现UIViewController?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52220590/