Reputation: 85
How can I present a ViewController from within a CollectionViewCell that is nested in a TableViewCell and that TableViewCell is inside a ViewController?
I have tried presentViewController
and performSegue
but they can't be called from within a cell.
Detailed explanation:
I have three files.
When someone taps on a story in CollectionViewCell then I want them to redirect to another ViewController i.e. SinglePostVC()
What I have tried:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("inside this")
self.window?.rootViewController?.present(SinglePostVC(), animated: true, completion: nil)
print("outside this")
}
Upvotes: 0
Views: 1883
Reputation: 1634
You can emit notification from PeopleCategoryCollectionViewCell and listen on it in ProfileViewController, then you can normally present view controller.
Tutorial for local notifications: https://useyourloaf.com/blog/local-notifications-with-ios-10/
Example:
NSNotificationCenter
.defaultCenter()
.postNotificationName(kCellTappedKey, object: nil, userInfo: nil)
And listen:
notificationCenter.addObserver(self,
selector: #selector(self.onCellTapped(_:)),
name: kDocumentCellTappedKey,
object: nil)
And add this function:
func onCellTapped(notification: NSNotification) { //call perform segue }
Upvotes: 0
Reputation: 9503
Use protocol and delegate like this
In tableViewCell
protocol CellDelegate {
func colCategorySelected(_ indexPath : IndexPath)
}
var delegate : CellDelegate?
In didSelect
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.colCategorySelected(indexPath)
}
In your ProfileViewController
class HomeVC: UIViewController , UITableViewDelegate, UITableViewDataSource, CellDelegate{
:
:
func colCategorySelected(_ indexPath : IndexPath){
// Push here
}
:
:
}
And dont forget
cellForRow
let Cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! tableCell
Cell.delegate = self // dont forget this line.
return Cell
Upvotes: 2