从UICollectionViewCell快速导航到UIViewController



我有麻烦了!我需要以编程方式设置从UICollectionViewCell (EVRecordingDetailsCollectionViewCell(到UIViewController(EVPersonalDataViewController(的导航。

我尝试过这种方式..但它说CollectionViewCell没有成员"存在">

let showloginViewController = EVPersonalDataViewController()
let ncShowloginViewController = UINavigationController(rootViewController: showloginViewController)
self.present(ncShowloginViewController, animated: true, completion: nil)

函数present(UIViewController, Bool, (() -> Void))是UIViewController的成员,而不是UICollectionViewCell的成员。

使用这种方式

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "yourCellId", for: indexPath) as! CustomCell
cell.parentViewController = self
// your cell setup...
return cell
}

在您的单元格类中保留对 UIViewController 的弱引用

class CustomCell: UICollectionViewCell {
weak var parentViewController: UIViewController?
@IBAction func onClickButton(sender: UIButton) {
let showloginViewController = EVPersonalDataViewController()
let ncShowloginViewController = UINavigationController(rootViewController: showloginViewController)
parentViewController?.present(ncShowloginViewController, animated: true, completion: nil)
}
}

相关内容

  • 没有找到相关文章

最新更新