Andrea Toso Andrea Toso - 3 months ago 9
Swift Question

Access to navigationController in a customCell

I have

collectionView
with customCells and in the first one (indexPath 0) I have a
Button
.



I'd want that when I click it, I go into another ViewController but I can't access to the
navigationController
because my cell is a
UICollectionViewCell
.

How can I get the access to the
navigationController
?

Answer

You can set the Button action pragmatically in your current ViewController, inside CollectionViewDataSource method cellForItemAtIndexPath for that cell this way.

cell.btnAdd.addTarget(self, action: #selector(self.buttonClicked(_:)), forControlEvents: .TouchUpInside)

After that add this method in your ViewController

func buttonClicked(sender: UIButton) {
    //perform navigation now
}