Swift – 将self.navigationController调用到自定义单元类中

迅速:

我有UICollectionViewController与另一个文件/类UICollectionViewCell

目标是从我的自定义单元类中推送UIViewController

像这样的东西:

 self.navigationController?.pushViewController(vc, animated: true) 

我没有问题从UICollectionViewController didSelectItemAtIndexPath实现push,但我想从注册到我的UICollectionViewController自定义单元类中执行此UICollectionViewController

当我尝试从自定义单元self.navigationController视图时,遗憾的是我没有访问self.navigationController

此外,我想100%以编程方式这样做。 我不使用Storyboard或Nib文件

提前致谢。

这是一个坏主意。 观点不应该/做那种逻辑。 你应该把它留给控制器(这就是MVC模式的内容)。

无论如何:

 class MyCollectionViewCell: UITableViewCell { var myViewController: MyViewController! } 

当单元格出列时你可以这样设置:

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell: MyCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(myCellIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell let nvc: UINavigationController = UIStoryboard(name: "myStoryboard", bundle: nil).instantiateViewControllerWithIdentifier("myNavigationController") as! UINavigationController cell.myViewController = nvc.childViewControllers.first as! MyViewController return cell } 

并在选择:

 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { let cell: MyCollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath) as! MyCollectionViewCell // I don't know where vc comes from cell.myViewController.navigationController?.pushViewController(vc, animated: true) } 

尽管如此,我无法想到这一点,这有什么意义。 因此,重新考虑您的架构。

通过在纸上绘制实体,可视化实体的通信。 您必须绘制模型视图控制器,并且只允许控制器与其他控制器 “对话”。

看看这个和这个

我最近也想出了这个问题,我有办法certificateAkhilrajtr的评论,因为它也可以帮助其他人。

首先在您的单元类中,您需要在文件顶部的协议:

 protocol YourCellDelegate: NSObjectProtocol{ func didPressCell(sender: Any) } 

然后在你的单元格类变量中添加:

 var delegate:YourCellDelegate! 

当您在单元格中执行某些操作时,触发协议的function:

 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { delegate.didPressCell(sender: indexPath) } 

在超级主控制器上,您应该符合您创建的代理:

 class MyViewController: UIViewController, YourCellDelegate{...} 

并且,实现协议的function,该function将在按下单元格时触发,就像之前定义的那样:

 func didPressCell(sender: Any){ let vc = SomeViewController() self.navigationController?.pushViewController(vc, animated: true) } 

当然不要忘记在cellForItem函数的cell实例化部分中为您的委托提供引用:

 cell.delegate = self 

我想扩展ezcoding的答案。 所以代替这个代码

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell: MyCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(myCellIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell let nvc: UINavigationController = UIStoryboard(name: "myStoryboard", bundle: nil).instantiateViewControllerWithIdentifier("myNavigationController") as! UINavigationController cell.myViewController = nvc.childViewControllers.first as! MyViewController return cell } 

您可以简单地使用self来获取视图控制器的当前实例,因此可以将代码简化为

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell: MyCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(myCellIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell cell.myViewController = self return cell } 

至少在我的情况下,这对我有用。