Reputation: 2832
I have a viewController where I have a tableview and inside every cell I have a collectionView.
I want to go to another view (where I will have a back button) so I tried to use my code which is working for the other views:
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)
But because this code is inside the TableViewCell I have an error that says:
Value of type 'HomeViewCell' has no member 'storyboard'
Is there any alternative parameter that I can use or something else it could do my task?
Thanks in advance.
Upvotes: 0
Views: 5124
Reputation: 1169
let detailOrderVC = UIViewController()
self.viewController()?.navigationController?.pushViewController(detailOrderVC, animated: true)
in swift 4, you need to pass the viewcontroller to the cell like below
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! CustomTableViewCell
// some your logic
cell.viewController = self // CustomTableViewCell needs to have viewController member variable
return cell
}
let detailOrderVC = UIViewController()
self.viewController?.navigationController?.pushViewController(detailOrderVC, animated: true)
Upvotes: 2
Reputation: 161
If you are pushing to a view class that requires layout you must call layout at the same time. ex.
let layout = UICollectionViewFlowLayout()
let nextViewclass = NextClass(collectionViewLayout: layout)
self.navigationController?.pushViewController(nextViewclass, animated: true)
Upvotes: 1
Reputation: 377
What you're missing is instantiating the storyboard:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
storyboard.instantiateViewControllerWithIdentifier("myNavigationController") as! UINavigationController
But as this stackoverflow answer says, this generally isn't a good MVC design.
You might want to look into delegate patterns or using NotificationCenter. Both these ways essentially trigger a call to the parent view which will then handle the corresponding action.
Upvotes: 0
Reputation: 42588
If you have a collection view inside of every cell, then I would recommend having each cell hold a container view. The view controller associated with the container view will have a storyboard.
The code for the push would look a bit ugly:
self.parent?.navigationController?.pushViewController(vc, animated: true)
The parent?.
goes from the contained view controller to the parent view controller.
Upvotes: 0