Wayne Filkins Wayne Filkins - 11 months ago 49
Swift Question

How do I pass an NSIndexPath through a button within a cell to another ViewController?

I have a button in every cell of a table. When the button is pressed it gets the NSIndexPath of the cell that button is in. The button is a segue to another view controller. How do I get that NSIndexPath into the new viewcontroller? If I use override func prepareforSegue I can't access the variable to pass it through because it is within the button function. I could probably make the variable accessible to the TableViewCell class, but I don't know how to declare an empty NSIndexPath or if you can do that, and also if it can be done it might lead to other problems. Or is that the best way to do it?

Answer Source

you can declare the variable accessible from everywhere like this:

var selectedIndexPath: NSIndexPath?