Ios 如何在单元格内使用pushViewController

Ios 如何在单元格内使用pushViewController,ios,swift,swift3,Ios,Swift,Swift3,我有一个viewController,其中有一个tableview,每个单元格中都有一个collectionView 我想转到另一个视图(在那里我将有一个后退按钮),因此我尝试使用我的代码,该代码适用于其他视图: let vc = self.storyboard?.instantiateViewController(withIdentifier: "detail") as! DetailView vc.name = beachesNames[indexPath.row] vc.

我有一个viewController,其中有一个tableview,每个单元格中都有一个collectionView

我想转到另一个视图(在那里我将有一个后退按钮),因此我尝试使用我的代码,该代码适用于其他视图:

let vc =  self.storyboard?.instantiateViewController(withIdentifier: "detail") as! DetailView
    vc.name = beachesNames[indexPath.row]
    vc.imagak = imagesNames[indexPath.row]
    self.navigationController?.pushViewController(vc, animated: true)
但由于该代码位于TableViewCell中,因此我有一个错误,即:

“HomeViewCell”类型的值没有成员“故事板”

是否有任何可供选择的参数,我可以使用或其他东西,它可以做我的任务


提前感谢。

如果每个单元格内都有一个集合视图,那么我建议每个单元格都有一个容器视图。与容器视图关联的视图控制器将具有故事板

推送的代码看起来有点难看:

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

父视图?
从包含的视图控制器转到父视图控制器。

如果每个单元格内都有一个集合视图,那么我建议每个单元格都有一个容器视图。与容器视图关联的视图控制器将具有故事板

推送的代码看起来有点难看:

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

父视图?
从包含的视图控制器转到父视图控制器。

您缺少的是实例化情节提要:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
storyboard.instantiateViewControllerWithIdentifier("myNavigationController") as! UINavigationController
但正如上面所说,这通常不是一个好的MVC设计


您可能希望查看委托模式或使用NotificationCenter。这两种方法本质上都会触发对父视图的调用,然后父视图将处理相应的操作。

您缺少的是实例化故事板:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
storyboard.instantiateViewControllerWithIdentifier("myNavigationController") as! UINavigationController
但正如上面所说,这通常不是一个好的MVC设计

您可能希望查看委托模式或使用NotificationCenter。这两种方法本质上都会触发对父视图的调用,然后父视图将处理相应的操作。

-swift 3 -斯威夫特4 在swift 4中,您需要将viewcontroller传递给单元格,如下所示

-斯威夫特3 -斯威夫特4 在swift 4中,您需要将viewcontroller传递给单元格,如下所示


如果要推送到需要布局的视图类,则必须同时调用布局。 前


如果要推送到需要布局的视图类,则必须同时调用布局。 前


只有UINavigationController可以按ViewController。只有UIViewController应具有UINavigationController。因此,一种方法是使用委托模式,这将允许告诉UIViewController在某个点上有单元(通常在UICollectionView内部,然后是UICollectionViewCell内部)将目标数据推送到VC。@Larme thanx您可以给出一个示例代码吗?@Larme我所做的是设置一个
var del:HomeViewController?
,并在我的
cellForRow
中将其设置为
cell.del=self
,然后在我的didselect(collectioview)中,我只是将self替换为del。你觉得这有什么好处?我会有问题吗?(它在没有任何警告的情况下工作)只有UINavigationController可以按下ViewController。只有UIViewController应具有UINavigationController。因此,一种方法是使用委托模式,这将允许告诉UIViewController在某个点上有单元(通常在UICollectionView内部,然后是UICollectionViewCell内部)将目标数据推送到VC。@Larme thanx您可以给出一个示例代码吗?@Larme我所做的是设置一个
var del:HomeViewController?
,并在我的
cellForRow
中将其设置为
cell.del=self
,然后在我的didselect(collectioview)中,我只是将self替换为del。你觉得这有什么好处?我会有问题吗?(它在没有任何警告的情况下工作)感谢您的回答,但我不太明白您的意思,您认为与容器视图关联的视图控制器将有一个故事板是什么意思@mikevorisis:是几年前的基础教程。谢谢你的回答,但我不太明白你的意思,你说与容器视图关联的视图控制器将有一个故事板是什么意思@mikevorisis:是几年前的基础教程。第二行中的“viewController()”是什么。第二行中的“viewController()”是什么。
let detailOrderVC = UIViewController()
self.viewController?.navigationController?.pushViewController(detailOrderVC, animated: true)
 let layout = UICollectionViewFlowLayout()
 let nextViewclass = NextClass(collectionViewLayout: layout)
 self.navigationController?.pushViewController(nextViewclass, animated: true)