On the FirstVC I have a variable index:
var index = -1 , which keeps the information of cell's indexPath.row so I can know in which cell the button was tapped. (Each cell has its own button)
I tried like this:
CellForRow:
cell.Btn.tag = indexPath.row cell.Btn.addTarget(self, action: #selector(detectButton), for: .touchUpInside) and then:
@objc func detectButton(sender: UIButton) { self.index = sender.tag } The secondVC is connected by the segue so I tried to send the index like this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "MySegue" { let vc = segue.destination as! SecondVC vc.element = self.elements[index] } } It crashes there because the index was still -1. I thought that @objc func detectButton will be called before prepareForSegue but it is not.
Why? And is there any other solution to send the index to the secondVC?
prepareForSegueto be called?